【问题标题】:EntityFrameworkCore MySql error in DotnetCore2.0 webapiDotnetCore2.0 webapi中的EntityFrameworkCore MySql错误
【发布时间】:2018-09-26 14:17:30
【问题描述】:

我正在 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)
);

【问题讨论】:

  • 你能贴出你如何访问 DbSet 的代码吗?
  • 酷,您可以在 GitHub 的 Entity Framework Core 存储库中提出问题。这样他们就可以修复它。

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


【解决方案1】:

想通了。

原来我引用的是 nuget 包 Microsoft.EntityFrameworkCore 版本 2.1.0-preview2-final。似乎与 MySql.Data.EntityFrameworkCore 存在兼容性问题。一旦我删除了第一个包,它就可以正常工作了。

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 2020-09-01
    • 2021-01-12
    • 2014-06-18
    • 2015-06-06
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2021-02-23
    相关资源
    最近更新 更多