.net核心1.1 entityframework和mysql數據庫連接

.net-core asp.net-core-webapi c# entity-framework-core mysql

我安裝vs 2017並嘗試使用.net core 1.1,實體框架和我的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類是

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:'實體類型'Categories'需要定義主鍵。

我不明白什麼是錯的。

請幫忙

更新1 Bellow是類別模型

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; }
    }

一般承認的答案

實體類型“類別” 需要定義主鍵

實體框架需要主鍵才能工作。有兩種方法可以做到這一點(我知道)。

例如

//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)]
Fluent: HasKey(emp => emp.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow