Tip: when using EF and fluent API, the context is not what it seems
Hi!
We are using the fluent API with EntityTypeConfiguration classes, and had lots of problems with LinqPad - basically, it was not picking up the config classes.
So if we had
LinqPad would not see it, and we would get the sql error: Invalid column name 'Name' when querying the blog. The code worked fine in the real program, of course.
It turned out the error was in our BloggingContext.OnModelCreating method.
We used
to find all our EntityTypeConfiguration classes.
But since the context is actually not a BloggingContext but a subclass made by linqpad,
Changing the code to
fixed it.
I hope posting this here will save others a few hours of frustration.
We are using the fluent API with EntityTypeConfiguration classes, and had lots of problems with LinqPad - basically, it was not picking up the config classes.
So if we had
Property(blog=>blog.Name).HasColumnName("BlogName");
LinqPad would not see it, and we would get the sql error: Invalid column name 'Name' when querying the blog. The code worked fine in the real program, of course.
It turned out the error was in our BloggingContext.OnModelCreating method.
We used
modelBuilder.Configurations.AddFromAssembly(this.GetType().Assembly);
to find all our EntityTypeConfiguration classes.
But since the context is actually not a BloggingContext but a subclass made by linqpad,
this.GetType().Assembly
doesn't find the correct assembly.Changing the code to
modelBuilder.Configurations.AddFromAssembly(typeof(BloggingContext).Assembly);
fixed it.
I hope posting this here will save others a few hours of frustration.