gpt4 book ai didi

c# - 在 EF Core 中添加迁移时出现 "The entity type ' CustomAttributeData ' requires a primary key to be defined"错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:47 27 4
gpt4 key购买 nike

我将模型放入带有 Core 的类库中。我已经进行了第一次迁移以测试我的模型的一部分,但在进行了重大改进之后,我删除了我的数据库和迁移以只有一个 V1 迁移。

问题是,在删除之后,当我尝试 add-migration 时出现此错误:

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);
}
}

谢谢。

最佳答案

我实际上想在我的模型中使用 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>();

关于c# - 在 EF Core 中添加迁移时出现 "The entity type ' CustomAttributeData ' requires a primary key to be defined"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com