gpt4 book ai didi

.net - .WithRequired 在 EF Fluent API 中扮演什么角色?

转载 作者:行者123 更新时间:2023-12-04 13:51:50 26 4
gpt4 key购买 nike

如果我有以下对象:

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 映射如下所示:
modelBuilder.Entity<Application>()
.HasMany(a => a.TestAccounts)
.WithRequired(t => t.Application)
.WillCascadeOnDelete(false);

这两者之间的关系是我可以拥有零个或多个 TestAccounts 的应用程序。

我试图描述两个表之间的 fk 关系。有人可以解释“.WithRequired”的作用。我不明白为什么需要这样做。

最佳答案

这意味着每个TestAccount实体必须有一个 Application与之关联的实体。我想一种表达方式是这样的:

如果在您的数据库中,您有另一个表的外键并且该外键不是 NULL,则使用 WithRequired , 否则如果可以为 NULL,则使用 WithOptional
以下是一些值得一看的文档:

http://msdn.microsoft.com/en-us/data/jj591620.aspx

关于.net - .WithRequired 在 EF Fluent API 中扮演什么角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15239488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com