ModelSnapshot.cs and Migration.Designer.cs doesn't compile due to undefined HasDiscriminator symbol
See original GitHub issueWhen adding migrations, the generated designer.cs and modelsnapshot.cs do not compile/build due to: “Error CS1929 ‘ReferenceOwnershipBuilder’ does not contain a definition for ‘HasDiscriminator’ and the best extension method overload ‘RelationalEntityTypeBuilderExtensions.HasDiscriminator<string>(EntityTypeBuilder, string)’ requires a receiver of type ‘EntityTypeBuilder’” for nested discriminators.
I commented out the offending lines in the generated code and it builds but have not done extensive testing on the impact and if commenting out the lines of generated code has any impact.
Steps to reproduce
ApplicationDbContext.cs:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Order>().OwnsOne(p => p.OrderInfo);
builder.Entity<Order>().OwnsOne(p => p.PaymentInfo);
builder.Entity<PaymentInfoA>().HasBaseType<PaymentInfoBase>();
builder.Entity<PaymentInfoB>().HasBaseType<PaymentInfoBase>();
builder.Entity<PaymentInfoC>().HasBaseType<PaymentInfoBase>();
builder.Entity<OrderInfoA>().HasBaseType<OrderInfoBase>();
builder.Entity<OrderInfoB>().HasBaseType<OrderInfoBase>();
builder.Entity<OrderInfoC>().HasBaseType<OrderInfoBase>();
}
}
Abstract classes + Concrete for TPH inheritance/mapping with disciminator
public abstract class PaymentInfoBase
{
public abstract string Name { get; protected set; }
}
class PaymentInfoA : PaymentInfoBase
{
public override string Name
{
get => nameof(PaymentInfoA);
protected set { }
}
}
class PaymentInfoB : PaymentInfoBase{
public override string Name
{
get => nameof(PaymentInfoB);
protected set { }
}
}
class PaymentInfoC : PaymentInfoBase {
public override string Name
{
get => nameof(PaymentInfoC);
protected set { }
}
}
public abstract class OrderInfoBase
{
public abstract string Name { get; protected set; }
}
class OrderInfoA : OrderInfoBase{
public override string Name
{
get => nameof(OrderInfoA);
protected set { }
}
}
class OrderInfoB : OrderInfoBase{
public override string Name
{
get => nameof(OrderInfoB);
protected set { }
}
}
class OrderInfoC : OrderInfoBase {
public override string Name
{
get => nameof(OrderInfoC);
protected set { }
}
}
public class Order
{
[Key]
public int Id { get; private set; }
public PaymentInfoBase PaymentInfo { get; set; }
public OrderInfoBase OrderInfo { get; set; }
}
The generated migration.Designer.cs/modelsnapshot.cs snippet (both files exhibit the same issue)
modelBuilder.Entity("WebApplication1.Models.Order", b =>
{
b.OwnsOne("WebApplication1.Models.OrderInfoBase", "OrderInfo", b1 =>
{
b1.Property<int?>("OrderId");
b1.Property<string>("Discriminator")
.IsRequired();
b1.Property<string>("Name");
b1.ToTable("Orders");
//compile/build error here
b1.HasDiscriminator<string>("Discriminator").HasValue("OrderInfoBase");
b1.HasOne("WebApplication1.Models.Order")
.WithOne("OrderInfo")
.HasForeignKey("WebApplication1.Models.OrderInfoBase", "OrderId")
.OnDelete(DeleteBehavior.Cascade);
});
b.OwnsOne("WebApplication1.Models.PaymentInfoBase", "PaymentInfo", b1 =>
{
b1.Property<int?>("OrderId");
b1.Property<string>("Discriminator")
.IsRequired();
b1.Property<string>("Name");
b1.ToTable("Orders");
//compile/build error here
b1.HasDiscriminator<string>("Discriminator").HasValue("PaymentInfoBase");
b1.HasOne("WebApplication1.Models.Order")
.WithOne("PaymentInfo")
.HasForeignKey("WebApplication1.Models.PaymentInfoBase", "OrderId")
.OnDelete(DeleteBehavior.Cascade);
});
});
EF Core version: 2.1 Preview 2 final Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2017 15.7 Preview 2
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Is this due to the limitation identified here: https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities?
“Current shortcomings - Inheritance of owned types is not supported”
Duplicate of #9148