sroughley
sroughley

Reputation: 77

EF Code First mapping an internal association

I have an issue that has me 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.

Any ideas why we are getting this?


Edit

Ladislav Mrnka, thanks for the help! This is still causing us pain.

We have already 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

Regards,

Stephen

Upvotes: 4

Views: 684

Answers (2)

user1035520
user1035520

Reputation: 21

Try with protected internal virtual.

Upvotes: 2

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

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

This is EF internals behavior. You have a class called Model.Question but EF internally has something called Repository.Question contained in the mapping - it was same with EDMX where you had a POCO class but the entity in the diagram had "different namespace".

It is possible that your internal navigation property wasn't mapped. Can you try to create database from your model and validate that relation between Answers and Questions tables is correctly set up? If not try to define this relation explicitly with fluent API.

Upvotes: 1

Related Questions