I am install vs 2017 and try to create a test web api project with .net core 1.1 ,entity framework and my sql database .
bellow is package list I am using
<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>
and dbcontext class is
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; }
}
but when I try to run an exception occurred
System.InvalidOperationException: 'The entity type 'Categories' requires a primary key to be defined.'
I don't understand whats going wrong.
please help
UPDATE 1 Bellow is the Categories Model
public class Categories
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public byte Picture { get; set; }
}
I already have the database created in MySql.
UPDATE 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; }
}
UPDATE 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 requires a primary key to work. There are two ways to do this (that I know of).
For example
//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);
}
}
Note that if your Primary Key is not auto-generated, you need to tell EF using whichever method you chose.
Annotation: [DatabaseGenerated(DatabaseGeneratedOption.None)]
Fluent: HasKey(emp => emp.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);