开发者

EF 4.1 Code First complex type with inheritance TPH

Is it possible to use a complex type of inheritance TPH in EF 4.1?

    [ComplexType]
    public class Unit
    {
        public double Value { get; set; }

        public Unit() { }

        public Unit(double Value)
        {
            this.Value = Value;
        }
    }

    public class Celsius: Unit
    {
       public Celsius(double Value) : base (Value)  { }

       public static explicit operator Kelvin(Celsius c)
       {
         return new Kelvin(c.Degrees + 273.16d);
       }

       [NotMapped]
       public double Degrees 
       {
         get { return this.Value; }
       }
    }

I use in this class association one-to-one: When I invoke() SaveChanges() raise Exception

public class Measurement
{
    [Key,
     Column("Id"),
     DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }
    public DateTime MTime { get; set; }
    public Unit Value { get; set; }
}

and Context:

class TestContext : DbContext
{

    public DbSet<Measurement> Measurements { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)     
    {
        try
        {
            modelBuilder.ComplexType<Unit>().Property(u => u.Value).HasColumnName("Value");

            modelBuilder.Entity<Unit>()
                        .Map<Celsius>(m => m.Requires("TypeName").HasValue("Celsius"))
                        .Map<Kelvin>(m => m.Requires("TypeName").HasValue("Kelvin"));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    } 
}

Is it possible to use complex types with inheritance one-table-hierarch开发者_如何学Cy in the FE 4.1 code first?


OK, I tested in EF 5 beta, however I think this should work the same. This is sort of a workaround, but one you might be able to live with. The discriminator portion does not seem to work when this is a complex type, so I initialized it in the respective constructors.

I have this and it works:


public class Measurement
{
    [Key,
        Column("Id"),
        DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }
    public DateTime MTime { get; set; }
    public virtual Unit Value { get; set; }
}

[ComplexType]
public class Unit
{
    protected Unit()
    {
    }

    protected Unit(double value)
    {
        Value = value;
    }

    [Column("Value")]
    public double Value { get; set; }

    [Column("TypeName")]
    public string TypeName { get; set; }
}

public class Celsius : Unit
{
    public Celsius(double value) : base(value)
    {
        TypeName = "Celsius";
    }

    public static explicit operator Kelvin(Celsius c)
    {
        return new Kelvin(c.Degrees + 273.16d);
    }

    public double Degrees
    {
        get { return this.Value; }
    }
}

public class Kelvin : Unit
{
    public Kelvin(double value) : base(value)
    {
        TypeName = "Kelvin";
    }

    public static explicit operator Celsius(Kelvin k)
    {
        return new Celsius(k.Degrees - 273.16d);
    }

    public double Degrees
    {
        get { return this.Value; }
    }
}

class TestContext : DbContext
{
    public DbSet<Measurement> Measurements { get; set; }
}

Here is the migrations code for this to see what is generated in the DB:


public partial class Initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Measurements",
            c => new
                {
                    Id = c.Long(nullable: false, identity: true),
                    MTime = c.DateTime(nullable: false),
                    Value = c.Double(nullable: false),
                    TypeName = c.String(),
                })
            .PrimaryKey(t => t.Id);

    }

    public override void Down()
    {
        DropTable("dbo.Measurements");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜