首页 文章

ado.net实体框架中的派生字段

提问于
浏览
0

我有一个看起来像这样的持久化类:

public partial class Unit
{
    public string Name { get; set; }
    public long LengthInMM { get; set; }
    public decimal VolumeCoefficient 
    {
        get { return LengthInMM * LengthInMM * LengthInMM; } 
    }
}

现在,派生字段(VolumeCoefficient)永远不会被明确分配 . 如何将其保存在ADO.NET实体框架中?

我想到了继承Unit类并重写getter和setter,但这看起来太乱了 .

1 回答

  • 1

    你为什么不直接宣布它是你的部分类的另一部分?

    由设计师生成:

    public partial class Unit
    {
        public string Name { get; set; }
        public long LengthInMM { get; set; }
    }
    

    在另一个文件中

    public partial class Unit
    {
        public decimal VolumeCoefficient 
        {
            get { return LengthInMM * LengthInMM * LengthInMM; } 
        }
    }
    

相关问题