-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TPT: Cannot create relationship between derived classes using base's relationship #22064
Comments
It's not throwing an error anymore, but it's creating both a |
You need to configure the base relationship or remove modelBuilder.Entity<ChildBase>()
.HasOne(c => c.BaseParent)
.WithMany()
.HasPrincipalKey(parent => parent.Id)
.HasForeignKey(c => c.BaseParentId);
You can remove it in the generated migration |
Yes, that worked. Thank you! |
@AndriySvyryd I didn't notice this cause I wasn't logging to console, but this is generating the warning from this: #22175
but I can't tell if there are actually any issues, or if I should ignore the error message for the record, the current context: public class MyContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Parent>().ToTable("Parent");
modelBuilder.Entity<ParentBase>().ToTable("ParentBase");
modelBuilder.Entity<Child>().ToTable("Child");
modelBuilder.Entity<ChildBase>().ToTable("ChildBase");
modelBuilder.Entity<ChildBase>()
.HasOne(c => c.BaseParent)
.WithMany()
.HasPrincipalKey(parent => parent.Id)
.HasForeignKey(c => c.BaseParentId);
modelBuilder.Entity<Child>()
.HasOne(c => c.Parent)
.WithMany(p => p.Children)
.HasPrincipalKey(parent => parent.Id)
.HasForeignKey(c => c.BaseParentId);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=localhost;Database=Test;Trusted_Connection=True;");
optionsBuilder.LogTo(Console.WriteLine);
}
}
public abstract class ParentBase
{
public Guid Id { get; set; }
}
public class Parent : ParentBase
{
public List<Child> Children { get; set; }
}
public abstract class ChildBase
{
public Guid Id { get; set; }
public ParentBase BaseParent { get; set; }
public Guid BaseParentId { get; set; }
}
public class Child : ChildBase
{
public Parent Parent { get; set; }
} |
@eraffel-MDSol You can ignore it, since you are configuring two relationships one of them will be created in the database |
I think the model will make it more obvious, but I'm having issues mapping the relationship between two derived classes, using the underlying relationship between the base classes.
Steps to reproduce
Try to create a migration using the following context and entities
When I try to create a migration, I get the following.
Further technical details
EF Core version: 5.0.0-rc.1.20414.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.7.1
The text was updated successfully, but these errors were encountered: