我安装vs 2017并尝试使用.net core 1.1,实体框架和我的sql数据库创建一个测试web api项目。
bellow是我正在使用的包列表
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0-preview1-final" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="7.0.7-m61" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
</ItemGroup>
</Project>
和dbcontext类是
public class NorthWindDBContext : DbContext
{
public NorthWindDBContext()
//: base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var configuration = builder.Build();
string connectionString = configuration.GetConnectionString("northwindConnection");
optionsBuilder.UseMySql(connectionString);
}
public virtual DbSet<Customers> Customers { get; set; }
public virtual DbSet<Categories> Categories { get; set; }
public virtual DbSet<Products> Products { get; set; }
public virtual DbSet<Orders> Orders { get; set; }
public virtual DbSet<Order_Details> Order_Details { get; set; }
}
但是当我尝试运行异常时
System.InvalidOperationException:'实体类型'类别' 需要定义主键。'
我不明白哪里出错了。
请帮助
更新1 贝娄是分类模型
public class Categories
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public byte Picture { get; set; }
}
我已经在MySql中创建了数据库。
更新2
public class Categories
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public byte Picture { get; set; }
}
更新3
public class Categories
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public byte Picture { get; set; }
}
答案 0 :(得分:3)
实体类型&#39;类别&#39; 需要定义主键。
实体框架需要主键才能工作。有两种方法可以做到这一点(我知道)。
例如
//Data Annotations
public class EmployeeEntity : BaseEntity
{
// Denotes the primary key column
[Key]
// Add this if your column is not auto-generated
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public short Id { get; set; }
public string Name { get; set; }
// more fields, probably
}
// Fluent API
public partial class EmployeeMap : EntityTypeConfiguration<EmployeeEntity>
{
public EmployeeMap()
{
ToTable("employee");
HasKey(emp => emp.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
请注意,如果您的主键未自动生成,则需要使用您选择的任何方法告知EF。
注释:[DatabaseGenerated(DatabaseGeneratedOption.None)]
流利:HasKey(emp => emp.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);