Skip to content

Navigation property combined with backing field leads to errors OnModelCreating #20238

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
APIWT opened this issue Mar 10, 2020 · 4 comments
Closed

Comments

@APIWT
Copy link

APIWT commented Mar 10, 2020

Take the following entities:

public sealed class Employee
{
    private Supervisor supervisor;

    public long Id { get; set; }

    public long? SupervisorId { get; set; }

    public Supervisor Supervisor
    {
        get => this.Department?.Supervisor ?? this.supervisor;
        set => this.supervisor = value;
    }

    public long? DepartmentId { get; set; }

    public Department Department { get; set; }
}

public sealed class Department
{
	public long Id { get; set; }
	
    public long? SupervisorId { get; set; }

    public Supervisor Supervisor { get; set;}
}

And the following DB context:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet<Employee> Employee { get; set; }
    public DbSet<Supervisor> Supervisor { get; set; }
    public DbSet<Department> Department { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Employee>()
            .Property(e => e.Supervisor)
            .HasField("supervisor")
            .UsePropertyAccessMode(PropertyAccessMode.Field);
    }
}

When running the update database command:

dotnet tool run dotnet-ef database update

The following error is generated:

The property 'Employee.Supervisor' is of type 'Supervisor' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

How do I combine both the backing field concept and the navigation property concept?

Further technical details

EF Core version: 3.1.2
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.1)

@APIWT APIWT added the type-bug label Mar 10, 2020
@APIWT
Copy link
Author

APIWT commented Mar 10, 2020

Just in-case this is not a bug, and I am doing something wrong, I have created a Stackoverflow post for additional feedback: https://stackoverflow.com/questions/60619040/in-entityframework-core-is-it-possible-to-use-a-navigation-property-combined-wi

@ajcvickers
Copy link
Contributor

@APIWT The answer to this depends on whether or not Supervisor is intended to be an entity type or not. If it is, then the references to it are "navigation properties" which are not configured using the Property API. See #6674 for how to configure the backing field.

If it's not an entity type, then use a value converter to map it to the database.

@APIWT
Copy link
Author

APIWT commented Mar 10, 2020

Hey @ajcvickers. Supervisor is intended to be an entity type. I'll check out your link. Thanks!

@APIWT
Copy link
Author

APIWT commented Mar 10, 2020

I'm going to close this issue, but I would recommend pointing out this information formally in the documentation. Thank you!

@APIWT APIWT closed this as completed Mar 10, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants