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

Error when setting default value for child classes in TPH #6117

Closed
scottste opened this issue Jul 20, 2016 · 0 comments
Closed

Error when setting default value for child classes in TPH #6117

scottste opened this issue Jul 20, 2016 · 0 comments
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

@scottste
Copy link

Steps to reproduce

Create a default template project with the individual accounts authentication. Change the ApplicationDbContext class to be:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Dog>()
            .Property(d => d.Selected)
            .HasDefaultValue(false);

        builder.Entity<Cat>()
            .Property(c => c.Selected)
            .HasDefaultValue(false);
    }

    public DbSet<Dog> Dogs { get; set; }

    public DbSet<Cat> Cats { get; set; }

    public DbSet<Animal> Animals { get; set; }
}

public abstract class Animal
{
    public int Id { get; set; }
}

public class Dog : Animal
{
    public bool Selected { get; set; }
}

public class Cat : Animal
{
    public bool Selected { get; set; }
}

Run Add-Migration initial

The issue

I get the following exception:

System.InvalidOperationException: 'Cat.Selected' and 'Dog.Selected' are both mapped to column 'Selected' in '.Animals' but are configured to use different default values ('False' and 'False').    at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
   at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.EnsureSharedColumnsCompatibility(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.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) 
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action`1 reporter) 
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0()
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
 'Cat.Selected' and 'Dog.Selected' are both mapped to column 'Selected' in '.Animals' but are configured to use different default values ('False' and 'False'). 

I would expect that since the default values are the same that this would be allowed. My current work around is to create an intermediate abstract class that has the property in it, but I would kind of prefer not do that in this situation. Is there another way to make the default values work in this situation?

Further technical details

EF Core version: 1.0.0
Operating system: Windows 10
Visual Studio version: VS 2015

@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 25, 2016
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 26, 2016
@AndriySvyryd AndriySvyryd removed their assignment Jul 26, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 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