我刚刚安装了Microsoft Visual Studio 2017社区,并试图了解最新技术。
我对MVC还不熟悉,并且对EF Core 1.1并不熟悉。
我已经在ASP.NET Core和EF Core上开了几个关于Pluralsight的课程,但是看起来这些录音发生了变化。
当我运行" Add-Migration Inital"为了连接数据库,我总是得到:
System.InvalidOperationException:实体类型'类别'需要定义主键。 在Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message) 在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.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_0
1.b__0() 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) 实体类型&#39;类别&#39;需要定义主键。
以下是一些相关代码:
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; }
}
当我构建项目时,我使用了目标框架.NETCoreApp 1.0,但我现在已将目标框架更改为.NetCoreApp 1.1。
答案 0 :(得分:0)
Microsoft.EntityFrameworkCore.Tools.DotNet
需要添加到您的项目中。右键单击该项目,然后选择Edit *.csproj
。然后,添加以下内容:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
</ItemGroup>
注意:该版本是本文发布时的最新版本,将来可能会发生变化。
现在,您可以开始创建迁移。转到项目的文件夹。右键单击项目和Open Folder in File Explorer
的最简单方法。然后,在cmd
的地址栏中键入File Explorer
,以在该文件夹中打开命令提示符。
现在使用以下命令创建迁移:
dotnet ef migrations add InitialCreate -c DokumentbasenContext
您现在应该在项目中看到Migrations
文件夹。