-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Inheritance does not discover Id as PK by conventions #7885
Comments
if use the Fluent API set the key, migration is ok. or property is "int Id" migration is ok. |
@ShenZZ I have not been able to repro this with just the information provided. Here is my attempt at a simple console app to show the issue. Can you provide a full project or code listing that demonstrates what you are seeing, perhaps using the listing below as a starting point? public class Program
{
public static void Main()
{
using (var context = new TestContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new AppUser { Id = "1" });
context.Add(new Employee { Id = "2" });
context.SaveChanges();
}
}
}
public class TestContext : DbContext
{
public DbSet<AppUser> AppUser { get; set; }
public DbSet<Employee> Employee { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Test;Trusted_Connection=True;ConnectRetryCount=0");
}
}
public class AppUser
{
public string Id { get; set; }
}
public class Employee : AppUser
{
} |
public class TestContext : DbContext
} this will throw exception ... |
@ShenZZ How is the class |
public class Program
{
public static void Main()
{
using (var context = new TestContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new AppUser { Id = "1" });
context.Add(new Employee { Id = "2" });
context.SaveChanges();
}
}
}
public class TestContext : DbContext
{
//public DbSet<AppUser> AppUser { get; set; }
public DbSet<AppUser> User { get; set; }
public DbSet<Employee> Employee { get; set; }
public TestContext(DbContextOptions<TestContext> options)
: base(options)
{
}
public TestContext()
: base()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Test;Trusted_Connection=True;ConnectRetryCount=0");
}
}
public class AppUser
{
public string Id { get; set; }
}
public class Employee : AppUser
{
} |
@ShenZZ Thanks for the repro! Note for triage: this code works if the AppUser DbSet property name is "AppUser": public DbSet<AppUser> AppUser { get; set; } but fails if the property name is "User" public DbSet<AppUser> User { get; set; } |
Reopening since the behavior described still seems to be a bug worth discussing in triage. |
@divega - This is bug. I was investigating this as I was surprised to see how come name of db set would ever affect KeyDiscoveryConvention. Based on ordering of discovery of Employee & AppUser, only one ordering works fine. It should be same in both cases. |
Thanks @smitpatel. |
Still not work for me?
The entity type 'Customer' requires a primary key to be defined. |
@silencieuxle - Your issue is much different from this issue, please file a new issue with repro code and version of EF Core you are using. |
TPH add-migration exception
Model:
Exception message:
System.InvalidOperationException: The entity type 'AppUser' requires a primary key to be defined.
Stack trace:
System.InvalidOperationException: The entity type 'AppUser' requires a primary key to be defined.
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(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.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(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.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.b__0()at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Steps to reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.
Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to repoduce the issue.
Further technical details
EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system:
IDE: Visual Studio 2015
The text was updated successfully, but these errors were encountered: