简体   繁体   English

我是否在多个实体框架有界上下文中添加了相同的表

[英]Do I add the same table in multiple entity framework bounded context

I have a fairly big database, about 80 tables or so. 我有一个相当大的数据库,大约有80个左右的表。 So I've decided to separate the tables into bounded context instead of having all 80 tables in 1 big edmx file. 所以我决定将表分成有界上下文,而不是在一个大的edmx文件中包含所有80个表。

So I have bounded contexts like Sales, Customers, etc. 所以我有像销售,客户等有限的背景。

I have my main customer table within my Customers edmx file. 我的客户edmx文件中有我的主要客户表。 However, I also need to access certain fields, not all, but 1 or 2 fields (eg, I just need customer name, instead of the whole customer object/table) from the customer table from the Sales edmx context. 但是,我还需要从Sales edmx上下文的customer表中访问某些字段,而不是所有字段,但只需要1或2个字段(例如,我只需要客户名称,而不是整个客户对象/表)。

Do I have to add the whole customer table into the Sales edmx file? 我是否必须将整个客户表添加到Sales edmx文件中? What's the best practice for this scenario? 这种情况的最佳做法是什么?

I like Julie Lerman's view on this topic http://msdn.microsoft.com/en-us/magazine/jj883952.aspx 我喜欢Julie Lerman对这个主题的看法http://msdn.microsoft.com/en-us/magazine/jj883952.aspx

I use bounded contexts for access performance since the load time of Contexts is faster when using smaller dbcontexts even when using generated views. 我使用有界上下文来获取访问性能,因为即使使用生成的视图,使用较小的dbcontexts时上下文的加载时间也会更快。 Simplyfying the access model is nice extra. 简单地访问访问模型是非常好的。 performance consider tips on MS ef site: http://msdn.microsoft.com/en-us/data/hh949853 性能考虑MS ef网站上的提示: http//msdn.microsoft.com/en-us/data/hh949853

BCs also allow other benefits such as restricting access to match business problem. BC还允许其他好处,例如限制访问以匹配业务问题。 The bigger issues arise if you try and work with db contexts that vary not just in which DBSet appear, but your try and alter the Model views. 如果您尝试使用不仅在DBSet出现的情况下变化的db上下文,而是尝试并更改模型视图,则会出现更大的问题。 I think that is best done outside of EF and mapped. 我认为最好在EF之外完成并映射。

I use one large SUPERSET Context to manage the DB creation/migration. 我使用一个大的SUPERSET Context来管理数据库创建/迁移。 But not Day to day access. 但不是日常访问。

Smaller DBcontexts are used for everyday all day access to data. 较小的DBcontexts用于日常全天访问数据。

So yes by all means use bounded contexts. 因此,无论如何都要使用有界上下文。 This can be against the SAME data store . 这可以针对SAME数据存储 And to answer the question as worded "Yes the same table ( DbSet) can appear in many contexts. 并回答问题,因为“是的,相同的表( DbSet)可以出现在许多情况下。

There are a couple of underlying issues with this approach (IMO): 这种方法存在一些潜在的问题(IMO):

EF, like any ORM, falls into the persistence concern. 与任何ORM一样,EF也属于持久性问题。 As such it should not be interfering with how you structure your domain model. 因此,它不应该干扰您构建域模型的方式。 The fact that you have all your entities persisted in what sounds like one persistence store may be an indication that your bounded contexts are overlapping or are non-existent. 事实上,您拥有所有实体,这听起来像一个持久性存储,可能表明您的有界上下文重叠或不存在。

Trying to move to a better structure is not a bad thing :) --- however, as Mark Oreta stated, I probably wouldn't bother with the EF structure. 试图转向更好的结构并不是件坏事:)但是,正如Mark Oreta所说,我可能不会为EF结构而烦恼。

The primary issue you are running into is caused by trying to read from your domain model. 您遇到的主要问题是尝试从您的域模型中读取。 This seems to crop up all too often in systems and it makes things rather difficult. 这似乎在系统中经常出现,并且使事情变得相当困难。 Lazy-loading is the direct result of exactly this. 延迟加载是这种情况的直接结果。 When you read you should, ideally, be using a query layer with access to a query store (may be the same database) that is optimized for reading. 当您阅读时,理想情况下,您应该使用一个查询层,该查询层可以访问为读取而优化的查询存储(可能是同一个数据库)。 In your example you need a denormalized customer name in your sales domain. 在您的示例中,您需要在销售域中使用非规范化的客户名称。 That is fine. 那样就好。 Trying to get that by reading your domain model and then trying to pull in data from a domain model in another bounded context is what is causing you the pain. 尝试通过阅读您的域模型然后尝试从另一个有界上下文中的域模型中提取数据来实现这一点,这正是导致您痛苦的原因。

If you go down the route of splitting your data then you will need to keep it split . 如果您沿着分割数据的路线走下去,那么您需要将其分开

Although the data from various BCs may all be in the same store you should think of your data as though each BC has its own database. 虽然来自各个BC的数据可能都在同一个商店中,但您应该将数据视为每个BC都有自己的数据库。 Sometimes I have a single project with various concerns (layers to some) in different folders/namespace (.NET here). 有时候我有一个项目,在不同的文件夹/命名空间(这里是.NET)中有各种各样的问题(某些层)。 It should be possible to split out those concerns into separate assemblies on a whim. 应该可以随心所欲地将这些问题分解成单独的组件。 So they should not be too tightly coupled. 所以他们不应该太紧密耦合。 The same goes for this data structure you are attempting to split. 您尝试拆分的数据结构也是如此。 You should in essence be able to split out the relevant BC data to a separate database. 实质上,您应该能够将相关的BC数据拆分到单独的数据库中。 The mechanisms for getting data across the BCs is another matter and I guess if that cannot be addressed you may find it tough going. 在BC中获取数据的机制是另一回事,我想如果无法解决这个问题,您可能会发现它很难实现。

Although I don't think that identifying BCs from the data side may be best idea it is certainly a step in the right direction :) 虽然我不认为从数据方面识别BC可能是最好的主意,但它肯定是朝着正确方向迈出的一步:)

If you're wanting to be able to access the Customer entity from your Sales entity using a navigation property, ( Sale.Customer ) you'll need to add it to the sales edmx. 如果您希望能够使用导航属性( Sale.Customer )从Sales实体访问Customer实体,则需要将其添加到sales edmx。

It's not necessarily a bad thing to have all your tables in a single edmx, as long as you create it, use it for the action(s) you want, and then dispose of it, the object graph won't get that large. 将所有表放在单个edmx中并不是一件坏事,只要你创建它,将它用于你想要的动作,然后处理它,对象图就不会那么大。

Alternatively, if you want to keep the bounded contexts, you could have a repository or something fetch it by ID when you need it. 或者,如果您想保留有界上下文,则可以在需要时使用存储库或其他内容来获取它。

var customer = customerContext.GetCustomerById(sale.CustomerId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM