Class Inheritance with .NET EF4.1 + MySQL

I am trying to implement class inheritance in .NET using Entity Framework 4.1 and MySQL as a database, with a code approach. The model below works with SQL Server, but crashes 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; } } 

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

Any help would be appreciated! Thanks.

UPDATE 1: I tried using the Table-Per-Hierarchy approach to relationships, 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 presented this as a bug for the MySQL development team because I don't know what else I can do. It seems that it should work, but it is not. Permanent link http://bugs.mysql.com/63920 .

UPDATE 3: I upgraded to NHibertnate 3.2 to verify this. Everything seems to be working fine. I would rather consider this ORM, but I would rather stay with EF.

UPDATE 4: In the official MySQL forum, I received a response stating that a fix for this error is under consideration. Should be fixed soon.

+3
source share
1 answer

Today I had the same problem, the difference is that I do not rely on Code First to create tables for me, I create them manually when I go.

I followed the description here , and I ended up with the same error as you, I manually created the Discriminator field to my table and changed its type to MEDIUMTEXT, but the provider insisted that I somehow provided the MaxLength property Although MEDIUMTEXT does not have MaxLength, such as VARCHAR, I assume this should be a vendor error.

Well, to get around this, I used the free API to manually specify the name of the discriminator column (which I just saved as the β€œDiscriminator”) and the values ​​that EF should expect from the discriminator column, in your case it 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 changed the column type of the discriminator to VARCHAR (50) without a visible problem, now it works well for me. An alternative for me would be to switch to another ORM that works too much, I will wait for the next versions of the MySql provider and test if they fixed it, in the meantime I will stick to this decision.

+1
source

All Articles