Entity Framework Code First and Multiple Assemblies

后端 未结 3 1605
遇见更好的自我
遇见更好的自我 2021-01-03 04:48

I have a subclass in a different assembly to its base class. The parent is a POCO class used for EF Code First.

When I try to add an instance of inherited class to t

相关标签:
3条回答
  • 2021-01-03 05:00

    I'm quite new to EF (Entity Framework 4) and I got the same exception when I made changes in the model.

    My problem turned out to be that I did not know EF need all the names on all the navigation properties to agree, not only their type. For example if there is a navigation property named foo, then there needs to be a declared variable in the corresponding class with the very same name.

    0 讨论(0)
  • 2021-01-03 05:07

    I know this post is a bit old, but I was able to accomplish this using @Dave's recomendation inside the constructor:

    public Context() {
        ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(
            System.Reflection.Assembly.GetAssembly(
                 typeof(--[Inherited DbContext]--)));
    }
    
    0 讨论(0)
  • 2021-01-03 05:10

    I solved this by inheriting from the first assembliy's DbContext, adding a DbSet<> for the derived class, and then adding new instances of derived type to to that.

    Posted code on MSDN forum here.

    0 讨论(0)
提交回复
热议问题