#

Asp.net Fehler: The entity type 'Products' requires a primary key to be defined.

 

Beim Erstellen der Migration in ASP.Net Core 2.0 MVC wird der Fehler ausgegeben, dass die Model-Klasse Products keinen Primary Key besitzt.

Man benötigt den PrimaryKey in jeder Tabelle auf SQL Server, damit die Datenbank schnell arbeiten kann.

 

Fehlermeldung in der Package Manager Console

PM> Add-Migration InitialCreate

The entity type 'Products' requires a primary key to be defined

 

Lösung: in den Model Klasse muss das Attribute [Key] zu einer Eigenschaft vom Typ Integer vorangesetzt werden.

 

Im Ordner Model befinden sich die Klassen, welche in der Regel die Tabellen einer Datenbank wiederspiegeln. Beim Bilden einer DbSet<Modelclass> in Entity Framework  wird die Modelklasse in eine DbSet-Tabelle gepackt.

Deshalb muss man hier nur vor das ID-Feld der Klasse das Attribug [Key] einfügen.

Damit [Key] erkannt wird, muss mit DataAnnotaions hinzugefügt werden mit

using System.ComponentModel.DataAnnotations;

 

hier die Datei Models/Products.cs als Beispiel

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Linq;

using System.Threading.Tasks;

 

namespace test_EF_Core2.Models

{

    public class Products

    {

        [Key]

        public int IDProduct { get; set; }

        public string Title { get; set; }

    }

}

 

Products.cs

 

Video Tutorial zur Lösung

https://www.youtube.com/watch?v=yx-GlRoJiRE

 

beim Anschliessenden erstellen der Migration und der anschliessenden Übertragen zum SQL Server sollte dann eine Tabelle mit einem ID Feld erstellt werden

erst Migration erstellen

PM> Add-Migration InitialCreate

Dadurch

 

Danach sollte unter /Migrations/datum_InitalCreate.cs die entsprechende Migrationsdatei stehen

 

Und dann die Struktur zum SQL Server hochladen

PM> Update-Database

 

 

Im SQL Server sollte dann die Tabelle dbo.Products mit dem T-SQL script zum Erstellen der Tabelle inklusive Primary Key

 

Und dem T-SQL script

CREATE TABLE [dbo].[Products] (

    [IDProduct] INT            IDENTITY (1, 1) NOT NULL,

    [Title]     NVARCHAR (MAX) NULL,

    CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ([IDProduct] ASC)

);

 

Wenn man diese Datenbank Tabelle im View Data öffnet, dann stellt man fest, dass die ID-Spalte automatisch als Long Integer hochgezählt wird

 

 

log:

PM> Add-Migration InitialCreate

Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]

      User profile is available. Using 'C:\Users\Raimund\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.

System.InvalidOperationException: The entity type 'Products' requires a primary key to be defined.

   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model)

   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model)

   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model)

   at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel model)

   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)

   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__0(Object k)

   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)

   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)

   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()

   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()

   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_1(IServiceProvider p)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)

   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)

   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)

   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)

   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()

   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()

   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()

   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)

   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)

   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)

   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.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()

   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 'Products' requires a primary key to be defined.

 

Mobile

.

123movies