У меня есть модель класса с двумя основными классами (PrincipalEntity1, principlentity2 ), которые оба связаны с одной и той же дочерним объектом (TwiceDependentEntity). Принципиальность2 напрямую связан с wquededletenceentity , а Principalentity1 содержит зависимость , что само по себе связано с wabedeed -upendentity :
public class PrincipalEntity1
{
public PrincipalEntity1() { }
public PrincipalEntity1(Guid id, string number, string name, DependentEntity dependent)
{
Id = id;
Number = number;
Name = name;
Dependent = dependent;
}
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public DependentEntity Dependent { get; set; }
}
public class DependentEntity
{
public DependentEntity() { }
public DependentEntity(Guid id, string name, TwiceDependentEntity twiceDependent)
{
Id = id;
Name = name;
TwiceDependent = twiceDependent;
}
public Guid Id { get; set; }
public string Name { get; set; } = null!;
public TwiceDependentEntity TwiceDependent { get; set; }
}
public class TwiceDependentEntity
{
public TwiceDependentEntity() { }
public TwiceDependentEntity(Guid id, string number, string name)
{
Id = id;
Number = number;
Name = name;
}
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
public class PrincipalEntity2
{
public PrincipalEntity2() { }
public PrincipalEntity2(Guid id, string number, string name, TwiceDependentEntity twiceDependent)
{
Id = id;
Number = number;
Name = name;
TwiceDependent = twiceDependent;
}
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public TwiceDependentEntity TwiceDependent { get; set; }
}
I'd like to know how I should configure such a model in EF Core 8.0, so that inserting, updating, and deleting principal entities containing children works without errors.
My current configuration leads to an exception during inserting an item of PrincipalEntity1:
SqlException: Die INSERT-ANWEISUNG Steht в Konflikt Mit der Foreign Key-einschränkung "fk_twiedEvendentEntentity_principalentity2_id".)modelBuilder
.Entity()
.ToTable(nameof(PrincipalEntity1))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder
.Entity()
.HasOne(x => x.Dependent)
.WithOne()
.HasForeignKey();
// ===============
modelBuilder
.Entity()
.ToTable(nameof(PrincipalEntity2))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder
.Entity()
.HasOne(x => x.TwiceDependent)
.WithOne()
.HasForeignKey();
// ===============
modelBuilder
.Entity()
.ToTable(nameof(DependentEntity))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder
.Entity()
.HasOne(x => x.TwiceDependent)
.WithOne()
.HasForeignKey();
// ===============
modelBuilder
.Entity()
.ToTable(nameof(TwiceDependentEntity))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
Подробнее здесь: https://stackoverflow.com/questions/797 ... same-child
Как настроить модель с двумя принципами, связанными с одним и тем же ребенком? ⇐ C#
Место общения программистов C#
1759309641
Anonymous
У меня есть модель класса с двумя основными классами (PrincipalEntity1, principlentity2 ), которые оба связаны с одной и той же дочерним объектом (TwiceDependentEntity). Принципиальность2 напрямую связан с wquededletenceentity , а Principalentity1 содержит зависимость , что само по себе связано с wabedeed -upendentity :
public class PrincipalEntity1
{
public PrincipalEntity1() { }
public PrincipalEntity1(Guid id, string number, string name, DependentEntity dependent)
{
Id = id;
Number = number;
Name = name;
Dependent = dependent;
}
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public DependentEntity Dependent { get; set; }
}
public class DependentEntity
{
public DependentEntity() { }
public DependentEntity(Guid id, string name, TwiceDependentEntity twiceDependent)
{
Id = id;
Name = name;
TwiceDependent = twiceDependent;
}
public Guid Id { get; set; }
public string Name { get; set; } = null!;
public TwiceDependentEntity TwiceDependent { get; set; }
}
public class TwiceDependentEntity
{
public TwiceDependentEntity() { }
public TwiceDependentEntity(Guid id, string number, string name)
{
Id = id;
Number = number;
Name = name;
}
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
public class PrincipalEntity2
{
public PrincipalEntity2() { }
public PrincipalEntity2(Guid id, string number, string name, TwiceDependentEntity twiceDependent)
{
Id = id;
Number = number;
Name = name;
TwiceDependent = twiceDependent;
}
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public TwiceDependentEntity TwiceDependent { get; set; }
}
I'd like to know how I should configure such a model in EF Core 8.0, so that inserting, updating, and deleting principal entities containing children works without errors.
My current configuration leads to an exception during inserting an item of PrincipalEntity1:
SqlException: Die INSERT-ANWEISUNG Steht в Konflikt Mit der Foreign Key-einschränkung "fk_twiedEvendentEntentity_principalentity2_id".)modelBuilder
.Entity()
.ToTable(nameof(PrincipalEntity1))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder
.Entity()
.HasOne(x => x.Dependent)
.WithOne()
.HasForeignKey();
// ===============
modelBuilder
.Entity()
.ToTable(nameof(PrincipalEntity2))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder
.Entity()
.HasOne(x => x.TwiceDependent)
.WithOne()
.HasForeignKey();
// ===============
modelBuilder
.Entity()
.ToTable(nameof(DependentEntity))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder
.Entity()
.HasOne(x => x.TwiceDependent)
.WithOne()
.HasForeignKey();
// ===============
modelBuilder
.Entity()
.ToTable(nameof(TwiceDependentEntity))
.HasIndex(k => k.Id)
.IsUnique();
modelBuilder
.Entity()
.Property(k => k.Id)
.HasDefaultValueSql("NEWID()");
Подробнее здесь: [url]https://stackoverflow.com/questions/79779755/how-to-configure-model-with-two-principals-related-to-the-same-child[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия