Code first中的ONE(Person)到MANY(PersonTypes)不会在DB中创建关系



我有一个人对象

public Person {
public string FullName { get; set;  }
public DateOfBirth DateOfBirth { get; set; }
public ICollection<PersonTypeLookup> PersonTypes {get; set;} 
}

我有一组预先确定的PersonTypeLookup类型。

public PersonTypeLookup(int id, string code, string displayName )
{
id = id;
this.Code = code;
this.DisplayName = displayName;

}

我希望一个人有很多不同的人类型,所以一个人怎么能有很多(人类型查找(怎么能做到,

public void Configure(EntityTypeBuilder<Person> configuration)
{ 
....
configuration.HasMany(i => i.PersonTypes).WithOne().OnDelete(DeleteBehavior.Cascade);
....
}

我试着将以上内容添加到Person配置中,但当我首先使用代码构建数据库时,数据库中的两个表之间没有设置链接,我需要这些信息吗?

一个人可以有多种类型吗?如果不是,那么这是一种一对多的关系,但情况正好相反。如果是的话,那么这是一种多对多的关系。

一对多设计:

public class Person {
public int Id { get; set; }
public string FullName { get; set;  }
public DateOfBirth DateOfBirth { get; set; }
public int PersonTypeId { get; set; }
public PersonType PersonType { get; set;} 
}
public class PersonType {
public int Id { get; set; }
public string Code { get; set; }
public string DisplayName { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}

多对多设计:

public class Person {
public int Id { get; set; }
public string FullName { get; set;  }
public DateOfBirth DateOfBirth { get; set; }
public virtual ICollection<PersonType> PersonTypes { get; set;} 
}
public class PersonType {
public int Id { get; set; }
public string Code { get; set; }
public string DisplayName { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}

相关内容

最新更新