DotnetCore2.0 webapi中的EntityFrameworkCore MySql错误

时间:2018-04-16 18:37:16

标签: mysql .net-core entity-framework-core

我正在使用dotnetcore 2.0 api上的EntityFrameworkCore MySql。

这是我得到的错误

Unable to cast object of type 'ConcreteTypeMapping' to type 'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping'.

我在尝试访问任何DbSet时遇到此错误。我无法追踪它是否是映射问题,连接问题或库问题。如果有人见过这个,请告诉我。否则这就是我到目前为止所做的。

public class GamerDbContext : DbContext
{
    public GamerDbContext()
    {

    }

    public GamerDbContext(DbContextOptions<GamerDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<GamerModel>();
    }

    public DbSet<GamerModel> GamerModel { get; set; }
    //public DbSet<GamerProfileModel> GamerProfiles { get; set; }
}

public class GamerModel
{
    [Key]
    public int Id { get; set; }

    //[Column(TypeName = "VARCHAR")]
    //[StringLength(36)]
    public string Username { get; set; }

    //[Column(TypeName = "VARCHAR")]
    //[StringLength(1024)]
    public string Password { get; set; }

    //[NotMapped]
    //public List<GamerProfileModel> GamerProfiles { get; set; }
}
-- auto-generated definition
CREATE TABLE Gamers
(
   Id       INT AUTO_INCREMENT PRIMARY KEY,
   Username VARCHAR(36)   NOT NULL,
   Password VARCHAR(1024) NULL,
   CONSTRAINT Gamers_Id_uindex
   UNIQUE (Id),
   CONSTRAINT Gamers_Username_uindex
   UNIQUE (Username)
);

2 个答案:

答案 0 :(得分:6)

想出来。

事实证明我正在引用nuget包Microsoft.EntityFrameworkCore Version 2.1.0-preview2-final。似乎与MySql.Data.EntityFrameworkCore存在兼容性问题。一旦我删除了第一个包,它工作正常。

答案 1 :(得分:0)

在NuGet软件包管理器中将所有版本设置为对我有用。

enter image description here