2017-05-29 1 views
0

Ich mag verursachen Beziehung machen, aber ich diesen Fehler:FOREIGN KEY-Einschränkung ‚Spalte‘ auf dem Tisch ‚Model‘ Einführung in Zyklen oder mehrere Kaskadenpfade

Introducing FOREIGN KEY constraint 'FK_dbo.MatchModels_dbo.LookingForaHomes_PropertyID' on table 'MatchModels' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

Ich habe zwei Modelle Eigenschaft hinzufügen und ich teile von 'PropertyType'

Zum Beispiel; matchModel hat 2 Zeilen;

MatchID 1 PropertyID 23 PropertyType 2 ....

MatchID 2 propertyID 23 PropertyType 1 ....

wenn PropertyType Wert 2 ist, seine für LookingForaHome oder Wert 1 ist es für LookingForaMate ist. Ist es wahr, wie ich weiß nicht, aber nach dieser Logik sollte ich Beziehung für

MatchModel machen: PropertyID -> LookingForaMate: propertyID

MatchModel: PropertyID -> LookingForaHome: propertyID

Wie kann ich machen Beziehung ?

public class LookingForaHome 
    { 
     [Key] 
     public int PropertyID { get; set; } 

    ... 


     public DateTime InsertionDate{ get; set; } 


     [Required] 
     public int UserID { get; set; } 


     public List<MatchModel> Match{ get; set; } 
     public virtual UserModel User { get; set; } 
     public virtual CityModel City { get; set; } 

     //Default Values 

     public LookingForaHome() 
     { 
      InsertionDate = DateTime.Now; 
     } 

    } 




public class LookingForaMateModel 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int PropertyID { get; set; } 

    .... 

    [Required] 
    public int UserID { get; set; } 


    public DateTime InsertionDate { get; set; } 

    public LookingForaMateModel() 
    { 
     InsertionDate = DateTime.Now; 
    } 

    public List<PropertyPhotoModel> PropertyPhoto { get; set; } 

    public List<MatchModel> Match { get; set; } 

    public virtual UserModel User { get; set; } 
    public virtual CityModel City { get; set; } 
} 




public class MatchModel 
    { 
     [Key] 
     public int MatchID { get; set; } 

     [Required] 
     public int PropertyID { get; set; } 
     [Required] 
     public int PropertyType { get; set; } 

     [Required] 
     public int UserID { get; set; } 

     [Required] 
     public bool IsSeen { get; set; } 

     [Required] 
     public DateTime InsertionDate { get; set; } 

     public virtual UserModel User { get; set; } 

     public virtual LookingForaMateModel LFMate { get; set; } 
     public virtual LookingForaHome LFHome{ get; set; } 

     public MatchModel() { 
      InsertionDate = DateTime.Now; 

     } 
    } 

Antwort

0

Einfachere Änderungen besteht darin, für jeden Typ zwei Nullable-Spalten zu verwenden.

public class MatchModel 
{ 
    [Key] 
    public int MatchID { get; set; } 

    [ForeignKey("LFMate")] 
    public int? MatePropertyID { get; set; } 
    [ForeignKey("LFHome")] 
    public int? HomePropertyType { get; set; } 
Verwandte Themen