none
EF Code First mapping an internal association RRS feed

  • Question

  • Hello all!

    We have an issue that has us stumped. We have a model assembly and a repositories assembly. The model layer contains classes that are mapped to our database by the DbContext derived repositories in the repositories layer. So far these have come along without a hitch.

    In the model we have a one to many association Question 1 - * Answer (Question.Answers and Answer.Question are the mapped navigation properties). We now have a requirement to mark Question.Answers as internal as this does not make sense outside of the model. When we make the change from: public virtual IList<Answer> ... to internal virtual IList<Answer> ... we get the exception:

    A specified Include path is not valid. The EntityType Repositories.Question does not declare a navigation property with the name Answers.

    First of all, this exception is odd as it references the type Repositories.Question, which does not exist! It should be Model.Question.

    The Model assembly already has InternalsVisibleTo set to Repositories:

    [assembly: InternalsVisibleTo("Repositories")]

    Which means that we do not get compile errors in the mapping code and we have tried the following:

    [assembly: InternalsVisibleTo("EntityFramework")]
    [assembly: InternalsVisibleTo("System.Data.Entity")]

    But we still get the aforementioned exception.

    We have defined the relationship using the fluent API:

    modelBuilder.Entity<Answer>()
       
    .HasRequired(a => a.Question)
       
    .WithMany(q => q.Answers)
       
    .Map(x => x.MapKey("QuestionId"));

    And this does work when the property q.Answers is set to public. The data is read and written to the database without issue. When we change it to internal, our code still compiles (as we're using InternalsVisibleTo) but we get the above exception when we run our integration tests.

    I have changed the connection string to a new catalog name and EF has no trouble creating the database and the expected constraints. So all is working as expected. The trouble only arises when changing the navigation property access modifier

    Any ideas?

    Regards,

    Stephen


    Monday, January 30, 2012 11:59 AM

All replies

  • Hi,

    I think you can try put model and repositories in same assembly to avoid the problem.


    I am fish.
    Wednesday, February 1, 2012 6:31 AM
  • Hey,

    Thanks for the reply. Unfortunately that is not an option as it would completely break the isolation and testability of our business logic layer.

    Regards,

    Stephen

    Tuesday, February 7, 2012 4:39 PM