使用.NET EF4.1 + MySQL进行类继承 [英] Class Inheritance with .NET EF4.1 + MySQL

查看:111
本文介绍了使用.NET EF4.1 + MySQL进行类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在.NET中使用Entity Framework 4.1和MySQL作为数据库来实现类继承,具有代码优先的方法。以下模型与SQL Server配合使用,但在MySQL中出现以下错误:

 指定的模式无效。错误:
(11,6):错误0064:不能为类型'mediumtext'指定方面'MaxLength'。

该模型是一个经典的简单示例:

  public abstract class Vehicle 
{
public int Id {get;组; }
public int Year {get;组;
}

public class Car:Vehicle
{
public string CarProperty {get;组;
}

public class Bike:Vehicle
{
public string BikeProperty {get;组; }
}

public class Db:DbContext
{
public DbSet< Vehicle>车辆组; }
public DbSet< Car>汽车组; }
public DbSet< Bike>自行车{get;组; }
}

对于SQL Server,它只创建一个名为车辆与列: Id 年份模型鉴别器;没有汽车或自行车的表。在MySQL中,数据库甚至没有创建(如果我删除Car类,它将创建 - 所以这不是许可的问题或类似的东西)。



任何帮助将会欣赏!谢谢。



更新1:
我尝试使用Table-Per-Hierarchy方法进行关系,切换到下面的上下文,但它给了我另一个错误:无法创建表'aimpa。#sql-da8_2c'(errno:150)

  public class Db:DbContext 
{
public DbSet< Vehicle>车辆组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Vehicle>()
.Map< Car>(o => ToTable(Cars))
.Map< Bike>(o => o.ToTable(Bikes));
}
}

更新2:
我已将此作为MySQL开发团队的错误提交,因为我不知道我还能做些什么。看来它应该是有效的,但它不行。永久链接是 http://bugs.mysql.com/63920



更新3:
我切换到NHibertnate 3.2,以测试这个。似乎工作很好我会更好地看看这个ORM,但我宁愿仍然在EF。



更新4:
在官方的MySQL论坛,我收到回复告诉他该错误的修复程序正在审查中。应该很快修复。

解决方案

我今天面临同样的问题,区别在于我不依赖代码首先为我创建表,我正在手动创建它们。



我已经遵循了这里,我最终在同样的错误,我手动创建了一个Discriminator字段在我的表,并将其类型更改为MEDIUMTEXT,但提供商坚持认为,我以某种方式提供一个MaxLength属性,甚至虽然MEDIUMTEXT没有像VARCHAR这样的MaxLength,但我认为这必须是提供者的一个错误。



嗯,为了解决这个问题,我用流畅的API手动告诉鉴别器列名称(我刚刚保留为鉴别器)和EF应该从歧视的期望值r列,在你的情况下,这将是:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Vehicle>()
.Map< Car>(m => m.Requires(Discriminator)HasValue(Car)
.Map< Bike>(m => m.Requires(Discriminator)。HasValue(Bike))
}

我已将我的鉴别器列的类型更改为VARCHAR(50)没有明显的问题,现在对我来说很好。对我来说,替代方案是迁移到另一个ORM,这是太多的工作,我会等待下一个版本的MySql提供程序,并测试是否已经解决了这个问题,同时我会坚持这个解决方案。 >

I'm trying to implement class inheritance in .NET using Entity Framework 4.1 and MySQL as database, with code-first approach. The model below works with SQL Server, but fails in MySQL with the following error:

Schema specified is not valid. Errors: 
(11,6) : error 0064: Facet 'MaxLength' must not be specified for type 'mediumtext'.

The model is a classic simple example:

public abstract class Vehicle
{
    public int Id { get; set; }
    public int Year { get; set; }
}

public class Car : Vehicle
{
    public string CarProperty { get; set; }
}

public class Bike : Vehicle
{
    public string BikeProperty { get; set; }
}

public class Db : DbContext
{
    public DbSet<Vehicle> Vehicles { get; set; }
    public DbSet<Car> Cars { get; set; }
    public DbSet<Bike> Bikes { get; set; }
}

With SQL Server, it creates only a table named Vehicles with columns: Id, Year, Model and Discriminator; no tables for Cars or Bikes. In MySQL, the database is not even created (if I remove the "Car" class, it creates - so it's not a problem of permission or something like that).

Any help would be appreciate! Thank you.

UPDATE 1: I tried using Table-Per-Hierarchy approach to the relationship, switching to the context below, but it gave me another error: Can't create table 'aimpa.#sql-da8_2c' (errno: 150).

public class Db : DbContext
{
    public DbSet<Vehicle> Vehicles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Vehicle>()
            .Map<Car>(o => o.ToTable("Cars"))
            .Map<Bike>(o=>o.ToTable("Bikes"));
    }
}

UPDATE 2: I've submitted this as a bug to MySQL development team, because I don't know what more can I do. It seems that it should work, but it doesn't. The permanent link is http://bugs.mysql.com/63920.

UPDATE 3: I switched to NHibertnate 3.2, in order to test this. Seems to work just fine. I'll take a better look at this ORM, but I would prefer still staying with EF.

UPDATE 4: In the official MySQL forum, I received a response telling that the fix for this bug is in review. Should be fixed soon.

解决方案

I was faced with the same problem today, the difference is that I'm not relying on Code First to create the tables for me, I'm creating them manually as I go.

I have followed the description given here and I ended up on the same error as you, I have manually created a "Discriminator" field on my table and changed its type to MEDIUMTEXT but the provider insisted that I was somehow providing a MaxLength property, even though MEDIUMTEXT has no MaxLength like VARCHAR, I assume this must be a bug on the provider.

Well, to work around this I have used fluent API to manually tell the discriminator column name (which I just kept as "Discriminator") and values that EF should expect from the discriminator column, in your case this would be:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Vehicle>()
            .Map<Car>(m => m.Requires("Discriminator").HasValue("Car"))
            .Map<Bike>(m => m.Requires("Discriminator").HasValue("Bike"));
}

I've changed the type of my discriminator column to VARCHAR(50) with no apparent problem, it's working well for me now. The alternative for me would be to migrate to another ORM which is just too much work, I'll wait for the next versions of the MySql provider and test if they have fixed this, meanwhile I'll stick to this solution.

这篇关于使用.NET EF4.1 + MySQL进行类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆