简体   繁体   中英

Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths

I want to make relationship but i getting this error:

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.

I have two model to add property and I divide by 'PropertyType'

For example; matchModel have 2 rows;

MatchID 1 PropertyID 23 PropertyType 2....

MatchID 2 PropertyID 23 PropertyType 1....

if PropertyType value is 2, its for LookingForaHome or value is 1 it's for LookingForaMate. Is it true way I dont know but according to this logic I should make relationship for

MatchModel: PropertyID -> LookingForaMate: PropertyID

MatchModel: PropertyID -> LookingForaHome: PropertyID

How can i make relationship ?

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;

        }
    }

Simpler changes is to use two nullable columns for each type.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM