Asp.net core 实体框架核心1.1和迁移

Asp.net core 实体框架核心1.1和迁移,asp.net-core,entity-framework-core,Asp.net Core,Entity Framework Core,我刚刚安装了Microsoft Visual Studio 2017社区,并尝试了解最新技术 我还不太熟悉MVC,也不太熟悉EF Core 1.1 我已经在ASP.NET Core和EF Core上学习了几门关于Pluralsight的课程,但自从录制这些课程以来,似乎已经发生了一些变化 当我运行“addmigrationinital”连接数据库时,我总是得到: System.InvalidOperationException:实体类型“类别”需要定义主键。 位于Microsoft.Entity

我刚刚安装了Microsoft Visual Studio 2017社区,并尝试了解最新技术

我还不太熟悉MVC,也不太熟悉EF Core 1.1

我已经在ASP.NET Core和EF Core上学习了几门关于Pluralsight的课程,但自从录制这些课程以来,似乎已经发生了一些变化

当我运行“addmigrationinital”连接数据库时,我总是得到:

System.InvalidOperationException:实体类型“类别”需要定义主键。 位于Microsoft.EntityFrameworkCore.Internal.ModelValidator.BursError(字符串消息) 位于Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel模型) 位于Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel模型) 位于Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext上下文、IConventionSetBuilder约定SetBuilder、IModelValidator验证器) 在System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey键,Func
2值工厂) 在Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()中 在Microsoft.EntityFrameworkCore.Internal.LazyRef
1.get_Value()中
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite ScopedCallSite,ServiceProvider提供程序)
在Microsoft.Extensions.DependencyInjection.ServiceProvider.c\u DisplayClass16\u 0.b\u 0(ServiceProvider provider)
位于Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider提供程序)
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite TransientCallSite,ServiceProvider提供程序)
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite ConstructorCallSite,ServiceProvider提供程序)
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite TransientCallSite,ServiceProvider提供程序)
在Microsoft.Extensions.DependencyInjection.ServiceProvider.c\u DisplayClass16\u 0.b\u 0(ServiceProvider provider)
位于Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider提供程序,键入serviceType)
位于Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider提供程序)
位于Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串名称、字符串输出目录、字符串上下文类型)
位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串名称、字符串输出目录、字符串上下文类型)
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0上
位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作)
实体类型“类别”需要定义主键

以下是一些相关代码:

C类

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);
    }
}
namespace Dokumentbasen6.Model
{
    public interface ICategoryRepository
    {
        IEnumerable<Category> Categories { get; }
        Category GetCategoryById(int CategoryId);
    }
}
名称空间Dokumentbasen6.Model
{
公共接口存储库
{
IEnumerable Categories{get;}
类别GetCategoryById(int CategoryId);
}
}
CategoryRepository.cs

namespace Dokumentbasen6.Model
{
    public interface ICategoryRepository
    {
        IEnumerable<Category> Categories { get; }
        Category GetCategoryById(int CategoryId);
    }
}
namespace Dokumentbasen6.Model
{
    public interface ICategoryRepository
    {
        IEnumerable<Category> Categories { get; }
        Category GetCategoryById(int CategoryId);
    }
}
名称空间Dokumentbasen6.Model
{
公共接口存储库
{
IEnumerable Categories{get;}
类别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; }
}
公共类DokumentbasenContext:DbContext
{
public DokumentbasenContext(DbContextOptions选项):base(选项)
{
}
公共DbSet Dokuments{get;set;}
公共数据库集类别{get;set;}
公共数据库集标准{get;set;}
公共数据库集成员{get;set;}
}

构建项目时,我使用了目标框架.NETCoreApp 1.0,但现在我已将目标框架更改为.NETCoreApp 1.1。

Microsoft.EntityFrameworkCore.Tools.DotNet
需要添加到项目中。右键单击项目并选择
Edit*.csproj
。然后,添加以下内容:

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

注意:该版本是本文发布时的最新版本,将来可能会更改

现在,您可以开始创建迁移了。转到项目的文件夹。最简单的方法是右键单击项目并在文件资源管理器中打开文件夹。然后,在
文件资源管理器的地址栏中键入
cmd
,打开该文件夹中的命令提示符

现在使用以下命令创建迁移:

dotnet ef迁移添加InitialCreate-c DokumentbasenContext

现在,您应该在项目中看到一个
Migrations
文件夹