Entity Framework, Code First and One-to-Many relationship across multiple contexts

最后都变了- 提交于 2019-11-29 11:41:07

You are working with so-called bounded contexts. The benefit of such contexts and how to work with them is explained in this blog by Julie Lerman.

The problem you experience, none of the contexts can be used in migrations, is addressed in this part:

If you’re doing new development and you want to let Code First create or migrate your database based on your classes, you’ll need to create an “uber-model” using a DbContext that includes all of the classes and relationships needed to build a complete model that represents the database.

Note that you can share the MyTrust type between all contexts, if you observe these rules (from Lerman & Miller's book DbContext, p 233):

  • An entity can only be attached to one context at a time. This architecture works best with short-lived contexts where the instance to be shared will be completely disassociated from one context before it is attached to another.
  • Entities that are attached to different contexts cannot be attached to one another.

UPDATE

In EF6 you can use multiple contexts for one migration path. See this walkthrough.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!