【发布时间】:2017-11-14 16:05:54
【问题描述】:
我有以下两个实体:
public class Company
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class Address
{
public Guid Id { get; set; }
public string Town { get; set; }
public string Country { get; set; }
}
这对应于两个数据库表:
Company:
Id uniqueidentifier
Name varchar
Address:
Id uniqueidentifier
Town varchar
Country varchar
RelationId uniqueidentifier
RelationId 是将链接存储回 CompanyId 的外键。
我不能更改类或表。
我试图弄清楚如何在代码优先的 EFCore 中表示此构造。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>().ToTable("Companies");
modelBuilder.Entity<Address>().ToTable("Addresses");
modelBuilder.Entity<Company>().HasKey(c => c.Id);
modelBuilder.Entity<Address>().HasKey(c => c.Id);
???????????????????????
}
我在上面的代码中缺少什么来防止创建 CompanyId 外键并改用“RelationId”。
【问题讨论】:
标签: c# entity-framework ef-code-first entity-framework-core