什么也部分的.WithRequired在EF流利的API玩? [英] What part does the .WithRequired play in an EF Fluent API?
问题描述
如果我有以下对象:
public class Application
{
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual ICollection<TestAccount> TestAccounts { get; set; }
}
public class TestAccount
{
public int TestAccountId { get; set; }
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual Application Application { get; set; }
}
EF映射是这样的:
EF Mapping looks like this:
modelBuilder.Entity<Application>()
.HasMany(a => a.TestAccounts)
.WithRequired(t => t.Application)
.WillCascadeOnDelete(false);
这两者之间的关系是,我可以申请零或多个TestAccounts。
The relationship between these two is that I can have applications with zero or many TestAccounts.
我想说明两个表之间的FK关系。有人可以解释什么是.WithRequired一样。我不明白为什么这是必要的。
I am trying to describe a fk relationship between the two tables. Can someone explain what the ".WithRequired" does. I don't understand why this is needed.
推荐答案
这意味着,每个 TestAccount
实体必须有一个应用程序$ C与它。我相关的$ C>实体想一个办法把它是这样的:
It means that each TestAccount
entity MUST have an Application
entity associated with it.I suppose one way to put it is like this:
如果你的数据库,你有一个外键到另一个表和外键不为空,然后使用 WithRequired
,否则,如果它可以为NULL,然后用 WithOptional
If in your DB, you have a foreign key to another table and that foreign key is NOT NULL, then use WithRequired
, else if it can be NULL, then use WithOptional
下面是一些文档值得看的:
Here is some documentation worth looking at:
<一个href="http://msdn.microsoft.com/en-us/data/jj591620.aspx">http://msdn.microsoft.com/en-us/data/jj591620.aspx
这篇关于什么也部分的.WithRequired在EF流利的API玩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!