简体   繁体   English

宣布DBSet <Type> 在DBcontext中 - 实体框架代码优先

[英]Declaring DBSet<Type> within DBcontext - Entity Framework Code First

When using EF Code First to generate a database, you declare the DB sets within the DbContext class for example.. 使用EF Code First生成数据库时,例如在DbContext类中声明数据库集。

public DbSet< ProductOption > ProductOptions { get; set; }

Now from the reading I have been doing, if you have a related entity in the type ProductOptions you do not need to declare it as EF will look at the dependents and use them to create the required DB tables. 现在从我一直在做的阅读中,如果你在ProductOptions类型中有一个相关的实体,你不需要声明它,因为EF将查看依赖项并使用它们来创建所需的数据库表。

My question is this: Is this recommend practice? 我的问题是:这是推荐做法吗? And if so how do you then access the related type in code as it does not exist as a data set. 如果是这样,那么如何访问代码中的相关类型,因为它不作为数据集存在。

Also this auto-discover feature of EF, does it do it in both directions, ie if you declare a db set that does not have any related entities inside it, but it is an entity that is in some other entity, will EF find it? 这也是EF的这种自动发现功能,它是否在两个方向上都这样做,即如果你声明一个内部没有任何相关实体的数据库集,但它是某个其他实体中的实体,EF会找到它?

Any clarification is welcome. 欢迎任何澄清。 Thanks 谢谢

EDIT 编辑

Example of what I am saying in terms of not being able to access the Types that are auto discovered is when your seeding data. 我所说的无法访问自动发现的类型的示例是您的播种数据。

You actually do have access to the types without declaring DbSets inside your context class. 实际上,您可以访问类型,而无需在上下文类中声明DbSets。 Inside your context initialization class under the Seed method you can access any of your entities using the yourContext.Set().Add(theObject). 在Seed方法下的上下文初始化类中,您可以使用yourContext.Set()访问任何实体。添加(theObject)。 This way you do not need to have DBSet properties inside your context class. 这样,您不需要在上下文类中包含DBSet属性。 I am not sure what version of EF you are using or if your are using an initialization class for seeding the data. 我不确定您使用的是哪个版本的EF,或者您是否正在使用初始化类来播种数据。 Regardless, you can still use the .Set().Add, to seed the data. 无论如何,你仍然可以使用.Set()。Add来播种数据。

With regards to the question will EF find the non related entities. 关于这个问题,EF将找到非相关实体。 Like you said, as long as an object is a property of an entity EF should create a table for it. 就像你说的,只要一个对象是一个实体的属性,EF就应该为它创建一个表。 As long as in code first you declare a mapping for any entity, it should create it and any child entities that may or may not have mappings for themselves. 只要在代码中首先声明任何实体的映射,它就应该创建它以及任何可能有也可能没有映射的子实体。 I'd have to see how you are declaring your context and mappings to be sure. 我必须看看你是如何声明你的背景和映射来确定的。

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

相关问题 首先使用Entity Framework 4代码在DbContext.DbSet中插入等效的InsertOnSubmit - InsertOnSubmit equivalent in DbContext.DbSet using Entity Framework 4 code first 实体框架中DBContext,DBSet &lt;&gt;的引用 - References for DBContext, DBSet<> in Entity Framework 实体框架代码首先添加 DbSet 属性 - Entity Framework code first adding DbSet property DbContext - > DbSet - >缺少Where子句(Entity Framework 6) - DbContext -> DbSet -> Where clause is missing (Entity Framework 6) 在Entity Framework中使用代码优先dbcontext进行选择时出现EntitySqlException - EntitySqlException on select with code first dbcontext in Entity Framework 在Code第一实体框架中将DbContext转换为Datatable - Convert DbContext to Datatable in Code first entity framework 如何在 Entity Framework Core 中使用复数 DbSet 属性名称搭建 DbContext? - How to scaffold DbContext with plural DbSet property names in Entity Framework Core? 使用 xUnit 动态测试所有 Entity Framework Core DbContext DbSet&lt;&gt; 对象 - Dynamically Test All Entity Framework Core DbContext DbSet<> objects with xUnit Entity Framework 6 和 DBContext 或 DBQuery 或 DBSet 或 ObjectQuery 与标量变量 - Entity Framework 6 and DBContext or DBQuery or DBSet or ObjectQuery with scalar variable 实体框架代码优先:两个DbContext和一个数据库初始化程序 - Entity Framework Code First : Two DbContext and one database initializers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM