dotnet / dotnet/EntityFramework.Docs

Add example for one to one with multiple type of class with same parent

Open
#4,486 0 comments 0 reactions 0 assignees View on GitHub
area-model-building
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

ITNOA

as you can see in https://stackoverflow.com/q/77083494/1539100 , I expect see some example in docs to resolve below problem

I'm working with Entity Framework Core (EF Core) and have encountered an issue related to lazy loading and inheritance. I have defined the following entity classes:

```csharp
public class Pool
{
public int Id { get; set; }
public virtual Profile? Profile { get; set; }
public int? AProfileId { get; set; }
public int? BProfileId { get; set; }
}

public abstract class Profile
{
public Profile() { }
public int Id { get; set; }
}

public class AProfile : Profile
{
public AProfile() { }
public virtual Pool? Pool { get; set; }
}

public class BProfile : Profile
{
public BProfile() { }
public virtual Pool? Pool { get; set; }
}

public class CProfile : Profile
{
public CProfile() { }
// CProfile does not have the Pool property
}

// Other profile classes
```

In my code, I've configured a relationship between `AProfile` and `Pool` using the `HasOne` method, and similarly for `BProfile`. I've also enabled lazy loading in my `OnConfiguring` method.

Here's how I've configured the relationships in the `OnModelCreating` method:

```csharp
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
.
.
.

builder.Entity().HasOne(P => P.Pool).WithOne(Q => Q.Profile as AProfile).HasForeignKey(Q => Q.AProfileId).IsRequired(false);

builder.Entity().HasOne(P => P.Pool).WithOne(Q => Q.Profile as BProfile).HasForeignKey(Q => Q.BProfileId).IsRequired(false);
}
```

However, when I retrieve a `Pool` object and try to access its `Profile` property, I notice that the `Profile` in `aPool` is null, while it should not be. The `Profile` in `bPool` works correctly.

```csharp
var aPool = dbContext.Pools.FirstOrDefault(p => p.Id == 1);

Console.WriteLine($"Profile in aPool is \"{aPool.Profile}\"; aPool.AProfileId is {aPool.AProfileId}");
```

The output shows that the `Profile` in `aPool` is null, while it should not be. The `Profile` in `bPool` works correctly.
The `AProfileId` and `BProfileId` field in both scenarios has value.

I also tried the other way around. I tried adding `aProfile` to `aPool.Profile` and adding `aPool` to `context.Pools` but it raised the following error:

`System.InvalidOperationException: 'The instance of entity type 'AProfile' cannot be tracked as the entity type 'BProfile' because the two types are not in the same hierarchy.'
`

**Additional Information:**

- .NET 6.0
- Entity Framework Core version: 6.0.21
- Database provider: Microsoft.EntityFrameworkCore.SqlServer 6.0.19
- EF Core Tools version: Microsoft.EntityFrameworkCore.Tools 6.0.19

I'm wondering why this issue is occurring and how to resolve it. Is there something specific I need to configure in EF Core to ensure that this works as expected in this inheritance scenario?

I Expect this to fill the Profile field with all of the Profile Types there.

Any insights or suggestions would be greatly appreciated.

---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: 8eb0a65d-4cc1-5f0a-2ffa-2875e9f9d586
* Version Independent ID: 8eb0a65d-4cc1-5f0a-2ffa-2875e9f9d586
* Content: [One-to-one relationships - EF Core](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-one)
* Content Source: [entity-framework/core/modeling/relationships/one-to-one.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/modeling/relationships/one-to-one.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**

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.