我开始使用并熟悉实体代码第一关系
我在使用EntityTypeConfiguration
将以下情况转换为代码时遇到了困难表
Family( Id pk int,
Name nvarchar)
Child( Id pk int,
FamilyId NULL constraint FK_Child_Family FOREIGN KEY REFERENCES Family (Id) DEFAULT NULL)
类
class Family
{
prop Id;
prop Name;
}
class Child
{
prop Id;
prop FamilyId;
virtual prop Family
}
我有家庭配置
private void Configure(EntityTypeConfiguration<Family> entity)
{
entity.ToTable(nameof(Family));
entity.HasKey(key => key.Id).Property(key => key.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); ///--> primary key configuration
entity.Property(prop => prop.Name).IsRequired();
}
我正在努力实现以下目标
家庭不了解儿童
孩子知道家庭并且有FamilyId(FK)
这是我目前为儿童配置
所拥有的private void Configure(EntityTypeConfiguration<Child> entity)
{
///--> only Child contains a Family FK
///--> need to map to FamilyId
///--> want to specify name of FK : "FK_Child_Family"
entity.HasOptional(user => user.Family) .... ?
}
谢谢