.net core 1.1 entityframework 和 mysql 数据库连接
.net core 1.1 entityframework and mysql database connection
我正在安装 vs 2017 并尝试使用 .net core 1.1、entity framework 和我的 sql 数据库创建一个测试 Web api 项目。
下面是我正在使用的包列表
<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 class 是
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: 'The entity type 'Categories'
requires a primary key to be defined.'
我不明白怎么回事。
请帮忙
更新 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; }
}
The entity type 'Categories' requires a primary key to be defined.
Entity Framework 需要主键才能工作。有两种方法可以做到这一点(据我所知)。
例如
//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);
我正在安装 vs 2017 并尝试使用 .net core 1.1、entity framework 和我的 sql 数据库创建一个测试 Web api 项目。
下面是我正在使用的包列表
<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 class 是
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: 'The entity type 'Categories' requires a primary key to be defined.'
我不明白怎么回事。
请帮忙
更新 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; }
}
The entity type 'Categories' requires a primary key to be defined.
Entity Framework 需要主键才能工作。有两种方法可以做到这一点(据我所知)。
例如
//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);