Как настроить модель с двумя принципами, связанными с одним и тем же ребенком?C#

Место общения программистов C#
Ответить
Anonymous
 Как настроить модель с двумя принципами, связанными с одним и тем же ребенком?

Сообщение 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()");


Подробнее здесь: https://stackoverflow.com/questions/797 ... same-child
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»