dotnet / dotnet/efcore

Improve exception messages for shared-type entity types

Open
#31,565 4 comments 0 reactions 0 assignees View on GitHub
area-dbcontext customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I have a many-to-many relationship...

```C#
public class ProductBacklogItem
{
...
public ICollection Parents { get; } = new List();
public ICollection Children { get; } = new List();
}

public class ProductBacklogItemDependency(Guid parentId, Guid childId)
{
public Guid ParentId { get; set; } = parentId;
public ProductBacklogItem Parent { get; private set; } = null!;
public Guid ChildId { get; set; } = childId;
public ProductBacklogItem Child { get; private set; } = null!;
}
```

Configured like this...

```C#
builder.HasMany(e => e.Parents)
.WithMany(e => e.Children)
.UsingEntity(
"ProductBacklogItemDependencies",
l => l.HasOne(e => e.Parent).WithMany().HasForeignKey(e => e.ParentId),
r => r.HasOne(e => e.Child).WithMany().HasForeignKey(e => e.ChildId),
j => j.HasKey(e => new { e.ParentId, e.ChildId })
);
```

I add a dependency like this...

```C#
dbContext.Set("ProductBacklogItemDependencies").Add(
new(productBacklogItemId, command.ProductBacklogItemId!.Value));
```

This works perfectly fine.

Now I change the dependency table to be a temporal table...

```C#
builder.HasMany(e => e.Parents)
.WithMany(e => e.Children)
.UsingEntity(
l => l.HasOne(e => e.Parent).WithMany().HasForeignKey(e => e.ParentId),
r => r.HasOne(e => e.Child).WithMany().HasForeignKey(e => e.ChildId),
j =>
{
j.ToTable("ProductBacklogItemDependencies", x => x.IsTemporal());
j.HasKey(e => new { e.ParentId, e.ChildId });
}
);
```

Now when I add the dependency, I get the error...

```
Cannot create a DbSet for 'Scrum.Api.Domain.ProductBacklogItemDependency' because this type
is not included in the model for the context. However the model contains an entity type with the
same name in a different namespace: 'Scrum.Api.Domain.ProductBacklogItemDependency'
```

EF Core version: 7.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.8.0 Preview 1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.