2017-04-21 14 views
0

私は2つのテーブルがあります:あなたはアクティビティテーブルに2つの外部キー(クラス)がある見ることができるようにEntityFrameworkコア流暢API

public class EstimationActivity 
{ 
    public Guid Id { get; set; } 

    public Guid EstimateId { get; set; } 
    public virtual Estimate Estimate { get; set; } 

    public Guid ParentActivityId { get; set; } 
    public virtual Activity ParentActivity { get; set; } 

    public Guid ActivityId { get; set; } 
    public virtual Activity Activity { get; set; } 

    public double Duration { get; set; } 

    [Range(minimum: 0, maximum: 100)] 
    public int Contingency { get; set; } 

    public ICollection<Note> Notes { get; set; } = new List<Note>(); 
} 

public class Activity 
{ 
    public Guid Id { get; set; } 

    public Guid ActivityTypeId { get; set; } 
    public virtual ActivityType ActivityType { get; set; } 

    public Guid RfcAreaId { get; set; } 
    public virtual RfcArea RfcArea { get; set; } 

    [Required] 
    [StringLength(maximumLength: 60, MinimumLength = 5)] 
    public string Name { get; set; } 
} 

:EstimationActivityとアクティビティを。私は、移行を追加しようとすると私が取得:

FOREIGN KEY制約を紹介

「FK_EstimateActivities_Activities_ParentActivityId」テーブルの「EstimateActivities」サイクルまたは複数のカスケードパスを引き起こす可能性があります。 NO DELETE NO ACTIONまたはUP UP NO NO ACTIONを指定するか、他のFOREIGN KEY制約を変更してください。

protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) 
     { 
      relationship.DeleteBehavior = DeleteBehavior.Restrict; 
     } 

     base.OnModelCreating(modelBuilder); 
    } 

これは仕事をしなかった:私はいくつかの記事で見られるよう

私は、これですべてのカスケード削除を制限しようとしました。エラーをどうやって解決すべきですか?

答えて

1

[ForeignKey("SomeId")]を使用してください。ここではFKを設定し、[InverseProperty("OtherClass")]はこの規則を無効にし、プロパティの配置を指定します。

public class EstimationActivity 
{ 
    public Guid Id { get; set; } 

    public Guid EstimateId { get; set; } 
    public virtual Estimate Estimate { get; set; } 

    public Guid ParentActivityId { get; set; } 

    [ForeignKey("ParentActivityId")] 
    public virtual Activity ParentActivity { get; set; } 

    public Guid ActivityId { get; set; } 

    [ForeignKey("ActivityId")] 
    public virtual Activity Activity { get; set; } 

    public double Duration { get; set; } 

    [Range(minimum: 0, maximum: 100)] 
    public int Contingency { get; set; } 

    public ICollection<Note> Notes { get; set; } = new List<Note>(); 
} 

public class Activity 
{ 
    public Guid Id { get; set; } 

    public Guid ActivityTypeId { get; set; } 
    public virtual ActivityType ActivityType { get; set; } 

    public Guid RfcAreaId { get; set; } 
    public virtual RfcArea RfcArea { get; set; } 

    [Required] 
    [StringLength(maximumLength: 60, MinimumLength = 5)] 
    public string Name { get; set; } 


    [InverseProperty("ParentActivity")] 
    public virtual EstimationActivity EstimationParentActivity { get; set; } 
    [InverseProperty("Activity")] 
    public virtual EstimationActivity EstimationActivity { get; set; } 
} 
+0

ありがとうございました。これは私の問題を解決した –