添加遷移EF Core時出現奇怪錯誤

asp.net-core c# ef-code-first entity-framework-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=localhost\SQLEXPRESS;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();

您可以在此鏈接上獲得更多信息: 使用CatFactory為EF Core生成代碼

這樣我們就可以避免在編寫EF Core代碼時出錯



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow