1
votes

I've just installed Microsoft Visual Studio 2017 Community and try to get heads up on the newest technology.

I'm not that familiar with MVC yet and definitely not familiar with EF Core 1.1.

I've taken a couple of courses on Pluralsight on ASP.NET Core and EF Core, but it seems there have been changes since these recordings where made.

When I run "Add-Migration Inital" to connect with the database, I always get :

System.InvalidOperationException: The entity type 'Category' 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.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.LazyRef1.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_01.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) The entity type 'Category' requires a primary key to be defined.

Here are some relevant code:

Category.cs

public class Category
{
    [Key]
    public int CategoryId;
    public String CategoryName;
}

ICategoryRepository.cs

namespace Dokumentbasen6.Model
{
    public interface ICategoryRepository
    {
        IEnumerable<Category> Categories { get; }
        Category GetCategoryById(int CategoryId);
    }
}

CategoryRepository.cs

namespace Dokumentbasen6.Model
{
    public interface ICategoryRepository
    {
        IEnumerable<Category> Categories { get; }
        Category GetCategoryById(int CategoryId);
    }
}

DokumentbasenContext.cs

public class DokumentbasenContext : DbContext
{
    public DokumentbasenContext(DbContextOptions<DokumentbasenContext> options) : base(options)
    {

    }
    public DbSet<Document> Dokuments { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Standard> Standards { get; set; }
    public DbSet<Member> Members { get; set; }
}

When I build the project I used the target framework .NETCoreApp 1.0, but I have now changed target Framework to .NetCoreApp 1.1.

1

1 Answers

1
votes

Microsoft.EntityFrameworkCore.Tools.DotNet needs to be added to your project. Right click the project and select Edit *.csproj. Then, add the following:

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
  </ItemGroup>

Note: the version is the latest at the time of this post and will likely change in the future.

Now, you can start to create the migrations. Go to the folder for your project. The easiest way it to right click the project and Open Folder in File Explorer. Then, type cmd in the address bar of the File Explorer to open a command prompt in that folder.

Now use the following command to create the migration:

dotnet ef migrations add InitialCreate -c DokumentbasenContext

You should now see a Migrations folder in your project.