我知道的唯一方法是使用主密钥。在此示例中,我将使用 Link 和 Company。您指出的模型:
public class Link
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string URL { get; set; }
public string CompanyName { get; set; }
}
我添加了 CompanyName,稍后会详细介绍。
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Link> Links { get; set; }
}
因此,您可能已经发现,即使子类中没有明确定义整数 FK 属性,EF Core 仍然会在数据库中创建它。
(据我所知)拥有一张完全不知道它与其他人的关系的表是不可能的。但是,使用主键,您可以将 FK 设置为引用父类中的属性而不是主键,为此我将CompanyName 添加到Link。它将引用Company 中的Name 属性并使用它来填充其值。这是您需要在 DbContext 类中进行的映射:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Link>()
.HasOne<Company>()
.WithMany(c => c.Links)
.HasForeignKey(l => l.CompanyName)
.HasPrincipalKey(c => c.Name);
}
这是应用迁移后生成的数据库架构 (SQL Server):
这是我用来测试的数据:
var newCompanies = new List<Company>()
{
new Company()
{
Name = "EF Core Enterprises",
Links = new List<Link>()
{
new Link()
{
Name = "Introduction",
Description = "The EF Core 101",
URL = "https://efcore.com/intro"
},
new Link()
{
Name = "Mapping",
Description = "Mapping classes to database objects",
URL = "https://efcore.com/mapping"
},
new Link()
{
Name = "Change Tracker",
Description = "Deep dive into EF Core's object tracking mechanism",
URL = "https://efcore.com/changetracker"
}
}
},
new Company()
{
Name = "ASP.NET Core Inc.",
Links = new List<Link>()
{
new Link()
{
Name = "Authentication",
Description = "Authentication Basics",
URL = "https://asp-net-core.com/auth"
},
new Link()
{
Name = "Routing",
Description = "Configure routing for web apps",
URL = "https://asp-net-core.com/routing"
},
new Link()
{
Name = "API",
Description = "Create a REST API with ASP.NET Core",
URL = "https://asp-net-core.com/API"
}
}
}
};
context.Companies.AddRange(newCompanies);
context.SaveChanges();
那么,是否可以使用Include() 为公司急切加载链接?是的,没有什么不同。
var companies = context.Companies
.Include(c => c.Links)
.ToList();
foreach (var company in companies)
{
foreach (var link in company.Links)
{
Console.WriteLine($"{link.CompanyName} --> {link.Name} at {link.URL}");
}
}
还有输出:
EF Core Enterprises --> Introduction at https://efcore.com/intro
EF Core Enterprises --> Mapping at https://efcore.com/mapping
EF Core Enterprises --> Change Tracker at https://efcore.com/changetracker
ASP.NET Core Inc. --> Authentication at https://asp-net-core.com/auth
ASP.NET Core Inc. --> Routing at https://asp-net-core.com/routing
ASP.NET Core Inc. --> API at https://asp-net-core.com/API
抱歉,篇幅太长,我只想尽可能清楚地解释所有内容。这是有关relationships 的文档。我希望你觉得这很有用。