繁体   English   中英

带有继承TPH的EF 4.1 Code First复杂类型

[英]EF 4.1 Code First complex type with inheritance 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代码中使用具有继承单表层次结构的复杂类型?

好的,我在EF 5 Beta中进行了测试,但是我认为这应该可以正常工作。 这是一种解决方法,但您可能可以使用。 当区分符部分是复杂类型时,它似乎不起作用,因此我在各自的构造函数中对其进行了初始化。

我有这个,它的工作原理:


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM