T
T
tuxcod2014-07-20 22:52:56
ASP.NET
tuxcod, 2014-07-20 22:52:56

EF CodeFirst: what causes an error when creating links?

There are models

public class PriceList
    {
        public PriceList()
        {
            PriceListId = Guid.NewGuid();
        }
        [Key]
        public Guid PriceListId { get; set; }
        public Guid PriceListCategoryId { get; set; }
        public string Name { get; set; }
        public int PriceMin { get; set; }
        public int PriceMax { get; set; }

        public virtual PriceListCategory PriceListCategory { get; set; }
        public virtual ICollection<ExecutiveUnitPriceList> ExecutiveUnitPriceLists{ get; set; }
    }

public class ExecutiveUnit
    {
        private ExecutiveUnit()
        {
            ExecutiveUnitId = Guid.NewGuid();
        }

        [Key]
        public Guid ExecutiveUnitId { get; set; }
        public Guid CompanyId { get; set; }
        public string Name { get; set; }
        public string Specialization { get; set; }
        public string Photo { get; set; }

        public virtual Company Company { get; set; }
        public virtual ICollection<ExecutiveUnitPriceList> ExecutiveUnitPriceLists{ get; set; }
    }

public class ExecutiveUnitPriceList
    {
        private ExecutiveUnitPriceList()
        {
            ExecutiveUnitPriceListId = Guid.NewGuid();
        }

        [Key]
        public Guid ExecutiveUnitPriceListId { get; set; }
        public Guid ExecutiveUnitId { get; set; }
        public Guid PriceListId { get; set; }
        public int RunTime { get; set; }

        public virtual ExecutiveUnit ExecutiveUnit { get; set; }
        public virtual PriceList PriceList{ get; set; }

    }

I add the latest model: ExecutiveUnitPriceList, I run a database update and the package manager gives an error:
Introducing a foreign key constraint (FOREIGN KEY) "FK_dbo.ExecutiveUnitPriceLists_dbo.PriceLists_PriceListId" on the table "ExecutiveUnitPriceListId" can lead to loops or multiple cascading paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or change other foreign key (FOREIGN KEY) constraints.
You cannot create a constraint.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2014-07-21
@Z10yTap0k

Most likely, you need to specify that the deletion of PriceList did not cascade the ExecutiveUnit. In this case, the child ExecutiveUnitPriceList are deleted cascaded.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ExecutiveUnitPriceList>()
        .HasRequired(a => a.ExecutiveUnit)
        .WillCascadeOnDelete(false);
}

O
Oleg, 2014-09-04
@plastilin1320

Can you tell me how to use a GUID for the primary key value type? I swear that you can use int.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question