我正在使用 Entity-Framework Core(版本"EntityFramework.Core": "7.0.0-rc1-final"
)由 SQL Server 2012 Express DB 支持。
我需要建立一个多对多关系模型Person
实体和一个Address
实体。
按照this http://ef.readthedocs.org/en/latest/modeling/relationships.html#many-to-many指南我用它建模了PersonAddress
join-table 实体,因为这样我可以存储一些额外的信息。
My goal就是这样设置我的系统:
- If a
Person
实例被删除,所有相关的PersonAddress
必须删除实例。一切Address
它们引用的实例也必须被删除,前提是它们与其他实例无关PersonAddress
实例。
- If a
PersonAddress
实例被删除,则Address
仅当与其他实例不相关时,才必须删除与其相关的实例PersonAddress
实例。全部Person
实例必须存在。
- If an
Address
实例被删除,所有相关的PersonAddress
必须删除实例。全部Person
实例必须存在。
我认为大部分工作必须在多对多的关系中完成Person
and Address
,但我也希望写一些逻辑。我将把这部分排除在这个问题之外。我感兴趣的是如何配置我的多对多关系。
这是当前的情况.
这是Person
实体。请注意,该实体与其他辅助实体具有一对多关系。
public class Person
{
public int Id {get; set; } //PK
public virtual ICollection<Telephone> Telephones { get; set; } //navigation property
public virtual ICollection<PersonAddress> Addresses { get; set; } //navigation property for the many-to-many relationship
}
这是Address
entity.
public class Address
{
public int Id { get; set; } //PK
public int CityId { get; set; } //FK
public City City { get; set; } //navigation property
public virtual ICollection<PersonAddress> People { get; set; } //navigation property
}
这是PersonAddress
entity.
public class PersonAddress
{
//PK: PersonId + AddressId
public int PersonId { get; set; } //FK
public Person Person {get; set; } //navigation property
public int AddressId { get; set; } //FK
public Address Address {get; set; } //navigation property
//other info removed for simplicity
}
这是DatabaseContext
实体,其中描述了所有关系。
public class DataBaseContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Address> Addresses { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
//All the telephones must be deleteded alongside a Person.
//Deleting a telephone must not delete the person it refers to.
builder.Entity<Person>()
.HasMany(p => p.Telephones)
.WithOne(p => p.Person);
//I don't want to delete the City when I delete an Address
builder.Entity<Address>()
.HasOne(p => p.City)
.WithMany(p => p.Addresses)
.IsRequired().OnDelete(Microsoft.Data.Entity.Metadata.DeleteBehavior.Restrict);
//PK for the join entity
builder.Entity<PersonAddress>()
.HasKey(x => new { x.AddressId, x.PersonId });
builder.Entity<PersonAddress>()
.HasOne(p => p.Person)
.WithMany(p => p.Addresses)
.IsRequired();
builder.Entity<PersonAddress>()
.HasOne(p => p.Address)
.WithMany(p => p.People)
.IsRequired();
}
}
Both Telephone
and City
为了简单起见,实体已被删除。
这是删除一个的代码Person
.
Person person = await _context.People.SingleAsync(m => m.Id == id);
try
{
_context.People.Remove(person);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
}
至于我的阅读避免.Include()
将让数据库处理最终的事情CASCADE
删除。抱歉,但我不记得澄清这个概念的 SO 问题。
如果我运行此代码,我可以使用以下方式为数据库播种这个解决方法 https://stackoverflow.com/a/35798149/6011874。当我想测试删除一个Person
具有上述代码的实体,我得到这个异常:
The DELETE statement conflicted with the REFERENCE constraint "FK_PersonAddress_Person_PersonId". The conflict occurred in database "<dbName>", table "<dbo>.PersonAddress", column 'PersonId'.
The statement has been terminated.
我测试了几种关系设置DatabaseContext.OnModelCreating
方法没有任何运气。
最后,这是我的question。我应该如何配置多对多关系才能正确删除Person
及其来自我的申请的相关实体,根据goal之前描述过?
谢谢你们。