question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModelSnapshot.cs and Migration.Designer.cs doesn't compile due to undefined HasDiscriminator symbol

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vflamecommented, Apr 23, 2018

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”

0reactions
ajcvickerscommented, May 24, 2018

Duplicate of #9148

Read more comments on GitHub >

github_iconTop Results From Across the Web

ModelSnapshot.cs and Migration.Designer.cs doesn' ...
When adding migrations, the generated designer.cs and modelsnapshot.cs do not compile/build due to:
Read more >
Entity Framework Core: Is it safe to delete Migration. ...
We have a database schema with ~200 tables. Model snapshot (Migration.Designer.cs) which is created for each migration is ~20K lines. So, having ...
Read more >
Entity Framework Core | PDF
Migrations to create a database from your model, and then evolve it as your model changes over time. using Microsoft.EntityFrameworkCore; using System.
Read more >
7d0a9ec5cef6b3ca8fc0f5d94f5a...
Automatic migrations. Work with existing databases. Customize Migrations history. Use Migrate.exe. Migrations in team environments. Use EF Designer
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found