当存在双重继承时,我总是遇到同样的问题:
public class Restriction
{
public int RestrictionID {get;set;}
public string Restriction_Name {get;set}
public int AssetID { get; set;}
public int SegmentID {get;set;}
public virtual Asset {get;set;}
public virtual Segment { get;set;}
}
public class Asset
{
[Key]
public int AssetID
public string Asset_Name
public virtual ICollection<Segment> Segments
}
public class Segment
{
[Key]
public int SegmentID
public string Segment_Name
[ForeignKey("Asset")]
public int AssetID
public virtual Asset Asset
public virtual ICollection<SubAsset> SubAssets
}
我试图阻止级联,但我再次遇到同样的错误:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Restriction>()
.HasRequired(f => f.Segment)
.WithRequiredDependent()
.WillCascadeOnDelete(false);
}
有人可以帮助我吗?我对这个问题感到很疯狂!
谢谢!
答案 0 :(得分:1)
基于Restriction,Asset和Segment类,所有外键都是必需的。
public class Asset
{
...
// Restrictions is missing
public virtual ICollection<Restriction> Restrictions { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Asset>()
.HasMany(e => e.Restrictions)
.WithRequired(e => e.Asset)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Asset>()
.HasMany(e => e.Segments)
.WithRequired(e => e.Asset)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Segment>()
.HasMany(e => e.Restrictions)
.WithRequired(e => e.Segment)
.WillCascadeOnDelete(false);
}