我剛剛安裝了Microsoft Visual Studio 2017社區,並試圖獲得最新技術。實體框架核心1.1和遷移
我對MVC還不熟悉,絕對不熟悉EF Core 1.1。
我已經參加了有關ASP.NET Core和EF Core上的Pluralsight的一些課程,但是看起來這些錄製之後有所變化。
當我運行「添加 - 遷移Inital」與數據庫連接,我總是得到:
System.InvalidOperationException:實體類型「類別」,要求定義主鍵。 在Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(字符串消息) 在Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel模型) 在Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel模型) 在Microsoft.EntityFrameworkCore .Infrastructure.ModelSource.CreateModel(的DbContext上下文,IConventionSetBuilder conventionSetBuilder,IModelValidator驗證器) 在System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func
2 valueFactory) 在Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() 在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.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.Opera tionBase.Execute(Action action) 實體類型'Category'需要定義一個主鍵。
這裏有一些相關的代碼:
Category.cs
public class Category
{
[Key]
public int CategoryId;
public String CategoryName;
}
ICategoryRepository.cs
命名空間Dokumentbasen6.Model { 公共接口ICategoryRepository { IEnumerable的分類{獲取; } 類別GetCategoryById(int CategoryId); }}
CategoryRepository.cs
namespace Dokumentbasen6.Model
{ 公共接口ICategoryRepository { IEnumerable的分類{獲取; } 類別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; }
}
當我建我使用的目標框架.NETCoreApp 1.0的項目,但現在我已經改變了目標框架來.NetCoreApp 1.1。
在此之前,非常感謝您的支持!
親切的問候,
喬恩·哈康Ariansen