@tuxcod

EF CodeFirst: из-за чего ошибка при создание связей?

Есть модели

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; }

    }


Добавляю последнюю модель: ExecutiveUnitPriceList, запускаю обновление базы и диспетчер пакетов выдает ошибку:

Введение ограничения внешнего ключа (FOREIGN KEY) "FK_dbo.ExecutiveUnitPriceLists_dbo.PriceLists_PriceListId" для таблицы "ExecutiveUnitPriceLists" может привести к появлению циклов или множественных каскадных путей. Укажите ON DELETE NO ACTION или ON UPDATE NO ACTION либо измените другие ограничения внешнего ключа (FOREIGN KEY).
Нельзя создать ограничение.
  • Вопрос задан
  • 5087 просмотров
Пригласить эксперта
Ответы на вопрос 2
Z10yTap0k
@Z10yTap0k
Скорее всего Вам необходимо указать, что при удалении PriceList не удалялся каскадно ExecutiveUnit. При этом дочерние ExecutiveUnitPriceList удаляются каскадно.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ExecutiveUnitPriceList>()
        .HasRequired(a => a.ExecutiveUnit)
        .WillCascadeOnDelete(false);
}
Ответ написан
Комментировать
plastilin1320
@plastilin1320
Подскажите как использовать GUID для типа значения primary key ? у меня ругается что можно использовать int.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы