我有一个带核心的类库,我把它放入模型中。 我已经进行了第一次迁移来测试我的模型的一部分,但经过一次大的改进后,我删除了我的数据库&迁移只有一个V1迁移。
在删除之后,当我尝试添加迁移时,我遇到了这个错误:
System.InvalidOperationException: The entity type 'CustomAttributeData' 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.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'CustomAttributeData' requires a primary key to be defined.
我不知道为什么会出现此错误,我的所有模型都有一个用 DataAnnotation 定义的主键
是否需要我可以提供新模型,但是我添加了上下文,如果与此相关或可能有帮助:
public class AmcContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Location> Location { get; set; }
public DbSet<Rating> Rating { get; set; }
public DbSet<RatingType> RatingType { get; set; }
public DbSet<Subscription> Subscription { get; set; }
public DbSet<Module> Module { get; set; }
public AmcContext(): base()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<User>();
modelBuilder.Entity<Client>().ToTable("Client");
modelBuilder.Entity<Professionnal>().ToTable("Professional");
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Database=Askmycar;Trusted_Connection=True;");
base.OnConfiguring(optionsBuilder);
}
}
感谢。
答案 0 :(得分:3)
在我的一个模型上,我有一个这样的属性:
public Type AvailableFor { get; set; }
Type是一个对象,因此它正在等待主键我将其更改为:
public string AvailableFor { get; set; }
及其作品。
感谢@ H.Herzi:)
答案 1 :(得分:1)
为了减少EF Core代码上的拼错错误,您可以使用代码生成器工具,EF Core有一个用于代码生成的命令行工具,在我的例子中,我使用CatFactory,并且使用这样的代码,我们可以从现有代码生成代码数据库:
var connectionString = "server=(local);database=Store;integrated security=yes;";
var dbFactory = new SqlServerDatabaseFactory()
{
ConnectionString = connectionString
};
var db = dbFactory.Import();
var project = new EfCoreProject()
{
Name = "Store",
Database = db,
OutputDirectory = "C:\\Temp\\Store"
};
project.BuildFeatures();
project
.GenerateEntities()
.GenerateAppSettings()
.GenerateMappingDependences()
.GenerateMappings()
.GenerateDbContext()
.GenerateContracts()
.GenerateRepositories();
您可以通过以下链接获取更多信息:Generating Code for EF Core with CatFactory
这样我们就可以避免在编写EF Core代码时犯错误
答案 2 :(得分:1)
我实际上想在模型中使用 Type ,并且我已经使用转换器初始化了属性:
modelBuilder.Entity<...>()
.Property(x => x.Type)
.IsRequired()
.HasConversion(
convertToProviderExpression: x => x.AssemblyQualifiedName,
convertFromProviderExpression: x => Type.GetType(x));
我认为这足以使Entity Framework Core认识到Type不是实体,但是我最终不得不显式地忽略它以使其起作用:
modelBuilder.Ignore<Type>();