Class Inheritance with .NET EF4.1 + MySQL

醉酒当歌 提交于 2019-11-29 08:10:05

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!