Skip to content
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

Metadata: Exception in model validation if derived type is discovered before base type #5547

Closed
smitpatel opened this issue May 27, 2016 · 4 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@smitpatel
Copy link
Contributor

Steps to reproduce

Model and context

public class MyContext : DbContext
{
    public DbSet<TicketBatch> TicketBatches { get; set; }
    public DbSet<PrintBatch> PrintBatches { get; set; }
    public DbSet<Ticket> Tickets { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;");
    }
}

public class TicketBatch
{
    [Key]
    public int Id { get; set; }
    public List<Ticket> Tickets { get; set; }
}

public class PrintBatch : TicketBatch
{
}

public class Ticket
{
    [Key]
    public Guid Guid { get; set; }

    public int TicketBatchId { get; set; }

    public TicketBatch TicketBatch { get; set; }
}

The issue

Model building fails.

using (var db = new MyContext())
{
    var model = db.Model;
}
Exception message: Exception: System.InvalidOperationException: The entity type 'PrintBatch' should derive from 'TicketBatch' to reflect the hierarchy of the corresponding CLR types.
Stack trace:
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.EnsureClrInheritance(IModel model, IEntityType entityType, HashSet`1 validEntityTypes)
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.EnsureClrInheritance(IModel model)
at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at ModelApp.Program.Main(String[] args)

If the name of the dbset of TicketBatches is changed to ATicketBatches (or anything which comes before PrintBatches alphabetically) it works fine.

Further technical details

EF Core version: RC2-final
Operating system: Win10
Visual Studio version: VS2015.2

Other details about my project setup:

@smitpatel
Copy link
Contributor Author

The issue here is [key] annotation sets key with CS.DataAnnotation which CS.Convention could not remove and doesn't set the base type conventionally.

@divega
Copy link
Contributor

divega commented May 27, 2016

Sounds to me like something we should try to fix for RTM. BTW, this is one of the issues we found in the repro we obtained from the customer for #5406.

@divega divega added this to the 1.0.0 milestone May 27, 2016
@divega
Copy link
Contributor

divega commented May 27, 2016

We agreed this should be fixed in RTM with @smitpatel @bricelam and @ajcvickers.

@RichardJDay
Copy link

Whats the actual fix for this ?
Value cannot be null. (Parameter 'builder')

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants