Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing discriminator column to int in Entity Framework 4.1

This is my situation, very much simplified.

My classes;

public class ClassBase
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
}

public class ClassMiddle1 : ClassBase
{

}

public class ClassMiddle2 : ClassBase
{
    public Guid Token { get; set; }
}

public class ClassA : ClassMiddle1
{
    public string UserId { get; set; }
    public string Username { get; set; }
}

public class ClassB : ClassMiddle2
{
    public string Username { get; set; }
}

And my OnModelCreating;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ClassBase>()
        .Map(m => {
            m.Properties(p => new { p.Id});
            m.ToTable("TableBase");
        });

    modelBuilder.Entity<ClassMiddle1>()
        .Map<ClassMiddle1>(m =>
        {
            m.Properties(p => new { });
            m.ToTable("TableBase");
        });

    modelBuilder.Entity<ClassMiddle2>()
        .Map<ClassMiddle2>(m =>
        {
            m.Properties(p => new { p.Token });
            m.ToTable("TableBase");
        });

    modelBuilder.Entity<ClassA>()
        .Map<ClassA>(m =>
        {
            m.Properties(p => new
            {
                p.UserId,
                p.Username
            });
            m.ToTable("TableA");

        });

    modelBuilder.Entity<ClassB>()
        .Map<ClassB>(m =>
        {
            m.Properties(p => new
            {
                p.Username
            });
            m.ToTable("TableB");

        }).Property(p => p.Username).HasColumnName("User");

}

This works fine but the Discriminator column is by default Discriminator, NVARCHAR(128). I read that it is possible to define this column myself using something like below.

m.Requires("ClassType").HasValue(1);

I turned my possibilities inside out but all times running into a dead end. Anyone having a suggestion how to do it?

I will end with another question. As our hierarchy pretty much are as above but even more derivated classes like C, D, E, F and so on to... say P. We found out that EF are making this incredibly big database query (~150K). Anyone else ran into this scenario?

I am hoping with changing Discriminator to at least minimize this. By that I say we have a very neat class hierarchy but an ugly query set.

like image 766
Per Avatar asked May 04 '11 21:05

Per


2 Answers

Late answer how the actual solution went. Only writing it down here because the documentation around this was not that easy to find.

My solution ended up like below...

modelBuilder.Entity<ClassBase>()
        .Map(m => {
            ...
            m.Requires("Discriminator").HasValue(1)
        });
like image 165
Per Avatar answered Sep 30 '22 07:09

Per


Regarding your "incredibly big database query": There are indeed performance and query generation issues with TPT inheritance mapping. There still doesn't seem to be a fix for those problems, only this vague announcement (August 2010):

The good news is that we are working on these issues so that EF no longer generates unnecessary SQL. The bad news is that it will take some time before the fix is delivered in a future release.

(Quote from the linked article above.)

like image 29
Slauma Avatar answered Sep 30 '22 06:09

Slauma