添加迁移EF Core时出现奇怪错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加迁移EF Core时出现奇怪错误相关的知识,希望对你有一定的参考价值。
我有一个带有核心的类库,我将它放入模型中。我已经进行了第一次迁移以测试我的模型的一部分,但经过大幅增强后,我删除了我的数据库并迁移到只有一个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=localhostSQLEXPRESS;Database=Askmycar;Trusted_Connection=True;");
base.OnConfiguring(optionsBuilder);
}
}
谢谢。
在我的一个模型上,我有一个这样的属性:
public Type AvailableFor { get; set; }
Type是一个对象,所以它等待主键我将其更改为:
public string AvailableFor { get; set; }
和它的作品。
感谢@ H.Herzi :)
要减少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代码时出错
我实际上想在我的模型中使用Type,并且我已经使用转换器初始化了属性:
modelBuilder.Entity<...>()
.Property(x => x.Type)
.IsRequired()
.HasConversion(
convertToProviderExpression: x => x.AssemblyQualifiedName,
convertFromProviderExpression: x => Type.GetType(x));
我认为这对于实体框架核心来说已经足够意识到Type不是一个实体,但我最终必须明确地忽略它以使其工作:
modelBuilder.Ignore<Type>();
以上是关于添加迁移EF Core时出现奇怪错误的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用 'dnx 时出现'MissingMethodException'。 EF7 beta6 中的 ef 迁移添加
尝试在 Web API Core 中使用 EF Core 在启动时更新数据库时出现“'无法访问已处置的对象”错误
如何首先将 EF Core 代码与 azure synapse 一起使用