<p>Hi fellow programmers! :)</p>
<p>I've just installed Microsoft Visual Studio 2017 Community and try to get heads up on the newest technology.</p>
<p>I'm not that familiar with MVC yet and definitely not familiar with EF Core 1.1.</p>
<p>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.</p>
<p>When I run "Add-Migration Inital" to connect with the database, I always get :</p>
<pre class="prettyprint ">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.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 'Category' requires a primary key to be defined.</pre>
<p>Here are some relevant code:</p>
<p><strong>Category.cs</strong></p>
<pre class="prettyprint "> public class Category
{
[Key]
public int CategoryId;
public String CategoryName;
}</pre>
<p><strong>ICategoryRepository.cs</strong></p>
<pre class="prettyprint ">namespace Dokumentbasen6.Model
{
public interface ICategoryRepository
{
IEnumerable<Category> Categories { get; }
Category GetCategoryById(int CategoryId);
}
}</pre>
<p>CategoryRepository.cs</p>
<pre class="prettyprint "> public class CategoryRepository : ICategoryRepository
{
private readonly DokumentbasenContext _context;
public CategoryRepository(DokumentbasenContext context)
{
_context = context;
}
public IEnumerable<Category> Categories => _context.Categories;
public Category GetCategoryById(int CategoryId)
{
return _context.Categories.FirstOrDefault(c => c.CategoryId == CategoryId);
}
}</pre>
<p></p>
<pre class="prettyprint "><strong>DokumentbasenContext.cs</strong></pre>
<pre class="prettyprint "> 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; }
}</pre>
<p></p>
<p>When I build the project I used the target framework .NETCoreApp 1.0, but I have now changed target Framework to .NetCoreApp 1.1.</p>
<p></p>
<p>In advance, thank you very much for your support!</p>
<p></p>
<p>Kind regards,</p>
<p>Jon Haakon Ariansen</p>