实体框架 4 代码优先:大型模型
在首先使用 EF4 代码处理大型数据库模型时,有人提出建议吗?实体集被添加到数据库上下文中,但如果我有 100 个表,我需要创建 100 个 DbSet,每个表 1 个:
public class Customers : DbContext
{
public DbSet<Customer> Customers {get; set;}
public DbSet<Employees> Employees {get; set;}
public DbSet<...
...
...
95 more
}
对于大型数据库模型,是否最好根据 DbSet 的域将 DbSet 拆分为多个类?
Has anyone advice when working with large database models when using EF4 code first? Entity sets are added to the database context, but if I have 100 tables, I need to create 100 DbSets, 1 for each table:
public class Customers : DbContext
{
public DbSet<Customer> Customers {get; set;}
public DbSet<Employees> Employees {get; set;}
public DbSet<...
...
...
95 more
}
With large database models, is it better to split the DbSets in to multiple classes depending on their domain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您必须为每个表创建 POCO 类和 DbSet。这就是代码优先的意义。如果您不喜欢它,您可以使用标准 EDMX,并让 EF 为您生成实体和映射。
将模型拆分为多个上下文可能是个好主意,但这取决于您的领域模型和解决方案的其他特征。请注意,延迟加载和复杂查询等许多功能仅适用于附加到同一上下文的实体。只有在上下文中映射的实体才能在那里使用。
对于如此大的解决方案,您可以检查有关聚合根和存储库的领域驱动设计理论。它可以为您提供一些有关拆分解决方案的想法。
Yes you have to create POCO class and DbSet for each table. That is the meaning of Code first. If you don't like it you can use standard EDMX and let EF generate entities and mapping for you.
Splitting model to multiple contexts can be good idea but it depends on your domain model and on other characteristics of your solution. Be aware that many features like lazy loading and complex queries works only on entities attached to the same context. Only entites mapped in the context can be used there.
For such a big solution you can check domain driven design theory about aggregate roots and repositories. It can give you some ideas about splitting your solution.