Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF 4.1带继承TPH的代码第一个复杂类型_C#_C# 4.0 - Fatal编程技术网

C# EF 4.1带继承TPH的代码第一个复杂类型

C# EF 4.1带继承TPH的代码第一个复杂类型,c#,c#-4.0,C#,C# 4.0,是否可以在EF 4.1中使用复杂类型的继承TPH? [ComplexType] 公营课组 { 公共双值{get;set;} 公共单元(){} 公共单位(双值) { 这个。值=值; } } 公共类摄氏度:单位 { 公共摄氏度(双倍值):基准(值){} 公共静态显式运算符开尔文(摄氏度c) { 返回新的开尔文(摄氏度+273.16度); } [未映射] 公共双学位 { 获取{返回此.Value;} } } 我在这个类关联中使用一对一: 调用()SaveChanges()时引发异常 public c

是否可以在EF 4.1中使用复杂类型的继承TPH?

[ComplexType]
公营课组
{
公共双值{get;set;}
公共单元(){}
公共单位(双值)
{
这个。值=值;
}
}
公共类摄氏度:单位
{
公共摄氏度(双倍值):基准(值){}
公共静态显式运算符开尔文(摄氏度c)
{
返回新的开尔文(摄氏度+273.16度);
}
[未映射]
公共双学位
{
获取{返回此.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);
        }
    } 
}
类TestContext:DbContext
{
公共数据库集度量{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
尝试
{
modelBuilder.ComplexType().Property(u=>u.Value).HasColumnName(“Value”);
modelBuilder.Entity()
.Map(m=>m.Requires(“TypeName”).HasValue(“摄氏度”)
.Map(m=>m.Requires(“TypeName”).HasValue(“开尔文”);
}
捕获(例外情况除外)
{
控制台写入线(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; }
}

好的,我在EF5测试版中进行了测试,但是我认为这应该是一样的。这是一种变通方法,但你可能可以接受。当这是一个复杂类型时,鉴别器部分似乎不起作用,所以我在相应的构造函数中初始化了它

我有这个,它可以工作:


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