2011-10-07 53 views
2

是否可以在EF 4.1中使用複雜類型的繼承TPH?EF 4.1代碼具有繼承性的第一種複雜類型TPH

[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; } 
     } 
    } 

我在這個類相關聯的一個對一使用: 當我調用()調用SaveChanges()提高異常

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

和背景信息:

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); 
     } 
    } 
} 

首先在FE 4.1代碼中使用具有繼承one-table-hierarchy的複雜類型是否可能?

回答

0

好的,我測試了EF 5測試版,但我認爲這應該是一樣的。這是一種解決方法,但你可能能夠忍受。當這是一個複雜的類型時,鑑別器部分似乎不工作,所以我在相應的構造函數中初始化它。

我也這樣與它的工作原理:


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; } 
} 

下面是該看什麼是在數據庫中生成的遷移代碼:


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"); 
    } 
} 
相關問題