问题描述
使用EF CTP5 code-首先我想映射类模型,其中包含多个集合在一个类中指向另一个类。下面是我的意思的例子:
With EF CTP5 Code-First I am trying to map a class model which contains multiple collections in one class pointing to another class. Here is an example of what I mean:
public class Company
{
public int CompanyId { get; set; }
public IList<Person> FemaleEmployees { get; set; }
public IList<Person> MaleEmployees { get; set; }
}
public class Person
{
public int PersonId { get; set; }
public Company Company { get; set; }
}
如果我让该模型与数据库中创建一个的DbContext
没有进一步的定制,例如:
If I let the database create from this model with a DbContext
without further customization, like so:
public class MyContext : DbContext
{
public DbSet<Company> Companies { get; set; }
public DbSet<Person> People { get; set; }
}
...然后我得到的SQL Server的两个表,一个简单的公司
表只用 CompanyId
列和人民
表具有以下列(FKRN的意思是外键关系名称,如EF在SQL Server中创建):
... then I get two tables in SQL Server, a simple Companies
table with only a CompanyId
column and a People
table with the following columns ("FKRN" means "Foreign Key Relationship Name", as created by EF in SQL Server):
PersonId int not nullable
CompanyCompanyId int nullable FKRN: Company_FemaleEmployees
CompanyCompanyId1 int nullable FKRN: Company_MaleEmployees
CompanyCompanyId2 int nullable FKRN: Person_Company
在最后三列有所有的外键关系中的主键 CompanyId
的公司
表。
现在我有几个问题:
-
1)为什么我得到的三外键在
人物列
表?其实,我认为的两个。如果删除了财产上市公司公司{获得;组; }
从人
第三列CompanyCompanyId2
消失,但我同时也失去了参考属性该类。
1) Why do I get three foreign key columns in the
People
table? I actually expected two. If I remove the propertypublic Company Company { get; set; }
from thePerson
the third columnCompanyCompanyId2
disappears but I also lose the reference property in the class.
2)比方说,我把从人
表中的公司
属性(我不知道需要它真的在我的模型)。有没有一种方法,让剩下的两个外键列另一个名字比自动创建 CompanyCompanyId
和 CompanyCompanyId1
? (例如 FCompanyId
和 MCompanyId
来表示相对于 FemaleEmployees
和 MaleEmployees
的集合。)
2) Let's say I drop the Company
property from the Person
table (I don't need it really in my model). Is there a way to give the two remaining foreign key columns another name than the auto-created CompanyCompanyId
and CompanyCompanyId1
? (For instance FCompanyId
and MCompanyId
to indicate the relation to the FemaleEmployees
and MaleEmployees
collections.)
3)有没有什么办法来定义这个模型只有一个人物外键
表?当然,我需要一个差异化的附加列在 CompanyId
人
类(如布尔IsFemale
)。一个人或者是部分的 FemaleEmployees
或 MaleEmployees
集合,从来没有在两者(当然在这个例子中),所以使用SQL我可以通过类似获取这些集合WHERE IsFemale = TRUE / FALSE,并且CompanyId = 1
。我想知道如果我可以给的EntityFramework一个提示加载两个集合这种方式。 (在这里,我想避免由 FemalePerson
和 MalePerson
类,无论是从<$ c中衍生出扩展模型$ C>人作为基类,然后使用例如表,每层次的映射,因为这些派生类是空的,人为的,并没有其他目的,除了使映射到SQL Server。)有只有一个外键 CompanyId
将允许我让非空的
这是不可能的两个外键(两者都可以从不是非空的在同一行中)。
3) Is there any way to define this model with only one foreign key CompanyId
in the People
table? Surely I would need a differentiating additional column in the Person
class (like bool IsFemale
). A Person is either part of the FemaleEmployees
or the MaleEmployees
collection, never in both (naturally in this example), so with SQL I could fetch those collections by something like WHERE IsFemale = true/false AND CompanyId = 1
. I am wondering if I could give EntityFramework a hint to load the two collections this way. (Here I would like to avoid to extend the model by a FemalePerson
and MalePerson
class which both derive from Person
as base class and then use for instance Table-Per-Hierarchy mapping, since these derived classes would be empty and artificial and had no other purpose except enabling the mapping to SQL Server.) Having only one foreign key CompanyId
would allow me to make it non-nullable
which isn't possible with two foreign keys (both can never be non-null in the same row).
感谢您的反馈和建议提前!
Thank you for feedback and suggestions in advance!
推荐答案
-
要质疑(1):EF不能映射类中的一个参考性
公司
人
来两种不同的采集终端FemaleEmployees
和MaleEmployees
类公司
的同一时间。映射约定假设有实际上是在公司
未在模型中暴露的第三个端点。因此,第三个外键被创建。
To question (1): EF cannot map the single reference property
Company
in classPerson
to two different collection endpointsFemaleEmployees
andMaleEmployees
in classCompany
at the same time. The mapping conventions assume that there is actually a third endpoint inCompany
which isn't exposed in the model. Therefore a third foreign key is created.要问(2):使用的的,现在可以通过使用指定流利的API中的外键(这是不可能的EF CTP5)的数据库列名的
地图
的ForeignKeyNavigationPropertyConfiguration
类的方法:To question (2): With the EF 4.1 Release Candidate it is now possible to specify the database column name of foreign keys in the Fluent API (which wasn't possible with EF CTP5) by using the
Map
method of theForeignKeyNavigationPropertyConfiguration
class:modelBuilder.Entity<Company>() .HasMany(c => c.FemaleEmployees) .WithOptional() .Map(conf => conf.MapKey("FCompanyId")) .WillCascadeOnDelete(false); modelBuilder.Entity<Company>() .HasMany(c => c.MaleEmployees) .WithOptional() .Map(conf => conf.MapKey("MCompanyId")) .WillCascadeOnDelete(false);
-
要问(3):我仍然不知道
To question (3): I still have no idea.
修改
只是为了现在关闭这个老问题:(3)(与于一体的实体两个导航属性到另一个实体的同一端点)是不可能的,例如:<一href="http://stackoverflow.com/questions/5342848/specific-entity-framework-$c$c-first-many-to-2-model-mapping/5343570#5343570">Specific实体框架code首先许多到2模型映射 ......(我记得这是寻找一个解决方案,这样的方案没有成功其他许多问题)
Just to close this old question now: (3) (relating two navigation properties in one entity to the same endpoint of another entity) is not possible, for example: Specific Entity Framework Code First Many to 2 Model Mapping ... (and I remember many other questions which were looking for a solution for such a scenario without success)
这篇关于实体框架CTP5 code-第一:映射一类与其他类的多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!