C
C#3d ago
Adam

✅ [solved] efcore owned entity issue

modelBuilder.Entity<A>(aBuilder =>
{
aBuilder.HasKey("Id");
aBuilder.Property<Guid>("Id");

aBuilder.OwnsMany<B>(
"bList",
bBuilder =>
{
bBuilder.HasKey("Id"); // Without it Ef core sets pair (AId, Id) as key but it is not needed - only one key (Id) is enough
bBuilder.Property<Guid>("Id");
bBuilder.Property<Guid>("AId").HasColumnName("AId");
bBuilder.WithOwner().HasForeignKey("AId");

bBuilder.OwnsOne<C>(b => b.C, cBuilder =>
{
cBuilder.Property<Guid>("Id");
cBuilder.Property<Guid>("BId").HasColumnName("BId");
cBuilder.WithOwner().HasForeignKey("BId");
});
}
);
});
modelBuilder.Entity<A>(aBuilder =>
{
aBuilder.HasKey("Id");
aBuilder.Property<Guid>("Id");

aBuilder.OwnsMany<B>(
"bList",
bBuilder =>
{
bBuilder.HasKey("Id"); // Without it Ef core sets pair (AId, Id) as key but it is not needed - only one key (Id) is enough
bBuilder.Property<Guid>("Id");
bBuilder.Property<Guid>("AId").HasColumnName("AId");
bBuilder.WithOwner().HasForeignKey("AId");

bBuilder.OwnsOne<C>(b => b.C, cBuilder =>
{
cBuilder.Property<Guid>("Id");
cBuilder.Property<Guid>("BId").HasColumnName("BId");
cBuilder.WithOwner().HasForeignKey("BId");
});
}
);
});
Unable to create a 'DbContext' of type 'DatabaseContext'. The exception 'The keys {'BId'} on 'C' and {'Id'} on 'B' are both mapped to 'B.PK_B', but with different columns ({'BId'} and {'Id'}).' was thrown while attempting to create an instance.
6 Replies
Sehra
Sehra3d ago
how would a key on only B.Id work since you say OwnsMany
Adam
AdamOP3d ago
class A
{
private List<B> bList = [];

public AddB(){
bList.Add(new B(Guid.NewGuid()));
}
}
class A
{
private List<B> bList = [];

public AddB(){
bList.Add(new B(Guid.NewGuid()));
}
}
A looks like this B.Id is always unique so it can be the only key afaik
Sehra
Sehra3d ago
class DB(DbContextOptions o) : DbContext(o)
{
protected override void OnModelCreating(ModelBuilder m)
{
m.Entity<A>(a =>
{
a.OwnsMany<B>(
x => x.Bs,
b =>
{
b.HasKey(x => x.Id);
b.OwnsOne(x => x.C);
}
);
});
}
}

class A
{
public Guid Id { get; set; }
public List<B> Bs { get; set; }
}

class B
{
public Guid Id { get; set; }
public A A { get; set; }
public C C { get; set; }
}

class C
{
public int Value { get; set; }
}
class DB(DbContextOptions o) : DbContext(o)
{
protected override void OnModelCreating(ModelBuilder m)
{
m.Entity<A>(a =>
{
a.OwnsMany<B>(
x => x.Bs,
b =>
{
b.HasKey(x => x.Id);
b.OwnsOne(x => x.C);
}
);
});
}
}

class A
{
public Guid Id { get; set; }
public List<B> Bs { get; set; }
}

class B
{
public Guid Id { get; set; }
public A A { get; set; }
public C C { get; set; }
}

class C
{
public int Value { get; set; }
}
CREATE TABLE "A" (
"Id" uuid NOT NULL,
CONSTRAINT "PK_A" PRIMARY KEY ("Id")
);
CREATE TABLE "B" (
"Id" uuid NOT NULL,
"AId" uuid NOT NULL,
"C_Value" integer,
CONSTRAINT "PK_B" PRIMARY KEY ("Id"),
CONSTRAINT "FK_B_A_AId" FOREIGN KEY ("AId") REFERENCES "A" ("Id") ON DELETE CASCADE
);
CREATE INDEX "IX_B_AId" ON "B" ("AId");
CREATE TABLE "A" (
"Id" uuid NOT NULL,
CONSTRAINT "PK_A" PRIMARY KEY ("Id")
);
CREATE TABLE "B" (
"Id" uuid NOT NULL,
"AId" uuid NOT NULL,
"C_Value" integer,
CONSTRAINT "PK_B" PRIMARY KEY ("Id"),
CONSTRAINT "FK_B_A_AId" FOREIGN KEY ("AId") REFERENCES "A" ("Id") ON DELETE CASCADE
);
CREATE INDEX "IX_B_AId" ON "B" ("AId");
Adam
AdamOP3d ago
thanks
Sehra
Sehra3d ago
$close
MODiX
MODiX3d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?