我们有以下型号:
public class Company
{
public int Id {get; set; }
public string Name {get; set; }
public CompanyRepresentative CompanyRepresentatives {get; set; }
}
public class CompanyRepresentative
{
public int Id {get; set; }
public string RepresentativeNames {get; set; }
}
CompanyRepresentative是一个SQL视图,使用流畅的API进行映射:
modelBuilder.Entity<Company>()
.HasOptional(q => q.CompanyRepresentatives)
.WithOptionalPrincipal()
.Map(q => q.MapKey("CompanyId"));
问题是,当删除公司时,EF正在尝试更新CompanyRepresentative,将CompanyId设置为NULL,因为它是一个View,显然会失败。
我们已尝试将映射更改为:
modelBuilder.Entity<Company>()
.HasOptional(q => q.CompanyRepresentatives)
.WithOptionalPrincipal()
.Map(q => q.MapKey("CompanyId"))
.WillCascadeOnDelete(false);
......没有效果。
有没有办法告诉EF在删除时不要尝试更新此属性?