Hi fellow programmers! :)
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.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.
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
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); } }
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.
In advance, thank you very much for your support!
Kind regards,
Jon Haakon Ariansen