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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…