Allow queries for mapped abstract base class with no discriminator

Open
#23,600 20 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
backend, database

Research direction

Start with the EF Core 5.0 model configuration and DbContext usage shown in the issue, then reproduce both SaveChanges and CountAsync with the supplied SQL Server setup. Done means determining the expected behavior for a mapped abstract base class without a discriminator and documenting or testing the confirmed failure.

Written by the indexing model from the issue text.

Description

area-model-building area-query customer-reported

Hi,
I am experiencing an issue while building model using EF Core (EF Core 5.0).
Console application (NET5) fails when trying to SaveChanges: "AMOS_ADDRESS is not mapped to a Table"
as reported in the attached screenshot.
Please can you help me in understanding what I am doing wrong?
Thanks

       var loggerFactory = LoggerFactory.Create(builder => {
            builder.AddFilter("Microsoft", LogLevel.Debug)
                   .AddFilter("System", LogLevel.Debug)
                   .AddFilter("ConsoleApp4.Program", LogLevel.Debug)
                   .AddConsole();
        });
        var optionBuilder = new DbContextOptionsBuilder();
        optionBuilder.UseLazyLoadingProxies(true);
        optionBuilder.UseLoggerFactory(loggerFactory);

        var modelBuilder = new ModelBuilder();
        modelBuilder.HasDefaultSchema("AMOS");
        modelBuilder.Entity<AMOS_ADDRESS>(b => b.ToTable("ADDRESS"));
        modelBuilder.Entity<AMOS_ADDRESS>().Property(e => e.CODE).HasColumnName("CODE");
        modelBuilder.Entity<AMOS_ADDRESS>().Property(e => e.ALPHACODE).HasColumnName("ALPHACODE");
        modelBuilder.Entity<AMOS_ADDRESS>().Property(e => e.ADDRESSID).HasColumnName("ADDRESSID");
        modelBuilder.Entity<AMOS_ADDRESS>().HasKey(e => e.ADDRESSID);

        modelBuilder.FinalizeModel();
        optionBuilder.UseModel(modelBuilder.Model);

        //optionBuilder.UseInMemoryDatabase(databaseName: "myDb");
        var connection = Microsoft.Data.SqlClient.SqlClientFactory.Instance.CreateConnection();
        connection.ConnectionString = "Data source=localhost;Initial catalog=ABS10.0.30;User ID=amos;Password=voyager";
        connection.Open(); // Works
        // To prove that table exists and connection works
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT COUNT(1) from AMOS.ADDRESS";
            var count = (int)command.ExecuteScalar();
            Console.WriteLine($"Address count {count}");
        }
        optionBuilder.UseSqlServer(connection);

        var dbContext = new myDbContext(optionBuilder.Options);
        dbContext.Addresses.Add(new AMOS_ADDRESS() { CODE = "001", ADDRESSID = 1, ALPHACODE = "A001" });
        dbContext.Addresses.Add(new AMOS_ADDRESS() { CODE = "002", ADDRESSID = 2, ALPHACODE = "A002" });
        dbContext.Addresses.Add(new AMOS_ADDRESS() { CODE = "003", ADDRESSID = 3, ALPHACODE = "A003" });
        dbContext.Addresses.Add(new AMOS_ADDRESS() { CODE = "004", ADDRESSID = 4, ALPHACODE = "A004" });
        dbContext.SaveChanges(); // FAILS AMOS_ADDRESS is not mapped to a table

        var addresses = dbContext.Addresses;
        Console.WriteLine(addresses.CountAsync().Result); // Fails AMOS_ADDRESS is not mapped to a table

image

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.