Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# 实体框架继承,是否可以根据具体类映射/不映射属性?_C#_.net_Entity Framework_Inheritance_Orm - Fatal编程技术网

C# 实体框架继承,是否可以根据具体类映射/不映射属性?

C# 实体框架继承,是否可以根据具体类映射/不映射属性?,c#,.net,entity-framework,inheritance,orm,C#,.net,Entity Framework,Inheritance,Orm,?在使用继承和实体框架时,是否可以根据具体类将某些属性定义为未映射 例如: public abstract class Transport {     [Key]     public Guid Id { get; set; }     public string PlateNumber { get; set; }     // [NotMapped] ??     public abstract int Length { get; set; } // This property may o

?在使用继承和实体框架时,是否可以根据具体类将某些属性定义为未映射

例如:

public abstract class Transport
{
    [Key]
    public Guid Id { get; set; }

    public string PlateNumber { get; set; }

    // [NotMapped] ??
    public abstract int Length { get; set; } // This property may or may not be mapped
}

public class Car : Transport
{
    public string Model { get; set; }

    // [MapThisOnePlease]
    public override int Length { get; set; } // For cars, I want this in the DB
}

public class Train : Transport
{
    public int WagonCount { get; set; }

    [NotMapped] // No mapping for trains, it is calculated
    public override int Length {
        get { return this.WagonCount * 15; }
        set { throw new NotSupportedException("Length is readonly for Trains"); }
    } 
}
所以我可以这样做:

int GetTransportLenght(Guid transportId) {
    Transport t = context.Transports.Where(t => t.Id == transportId).First();
    return t.Length;
}
我也想做这样的事情:

List<Car> GetCarsLongerThan(int length) {
    return context.Cars.Where(c => c.Length > length).ToList();//if I try this with a train EF won't be happy
}

关于和的相关问题似乎是重复的如果不希望实体表具有指定的列,可以忽略实体框架fluent api配置中的属性。示例:modelBuilder.Entity().Ignore(x=>x.TotalLength)不会将TotalLength属性添加到表中
public abstract class Transport
{
    [Key]
    public Guid Id { get; set; }

    public string PlateNumber { get; set; }

    [NotMapped]
    public abstract int TotalLength { get; }
}

public class Car : Transport
{
    public string Model { get; set; }

    public int Length { get; set; }

    public override int TotalLength { get { return this.Length; } }
}

public class Train : Transport
{
    public int WagonCount { get; set; }

    public override int TotalLength { get { return this.WagonCount * 15; } }
}