我正在编写一个应用程序,我们需要将一个基本实体扩展到许多不同的事物(例如员工、车辆等)。设计是这样的,有一个实体表和第二个具有特定类型值的表,例如,雇员将有一个 ID 号,但车辆将有一个注册号。

我继承了数据上下文中生成的类实体,但在我的存储库中进行转换时遇到了问题。这样做的正确方法是什么?

public class cAccountEmployee : cAccountEntity
{
    public string id_number
    {
        get
        {
            try
            {
                return this.cAccountEntityValues.Single(e => e.type == 1).value;
            }
            catch (Exception)
            {
                return "";
            }
        }

        set
        {
            try
            {
                this.cAccountEntityValues.Single(e => e.type == 1).value = value;
            }
            catch (Exception)
            {
                this.cAccountEntityValues.Add(new cAccountEntityValue()
                                            {
                                                accountentity_id = this.id,
                                                cAccountEntity = this,
                                                type = 1,
                                                value = value
                                            });
            }
        }
    }

}

然后在我的存储库中(不继承任何内容)

public IEnumerable<cAccountEmployee> All(int accountholder_id)
    {
        return db.cAccountEntities.Where(e => e.accountholder_id == accountholder_id).OrderBy(a => a.name).Cast<cAccountEmployee>();
    }

    public cAccountEmployee Single(int id)
    {
        return db.cAccountEntities.Single(a => a.id == id) as cAccountEmployee;
    }

单一方法中的转换失败,因此我返回 null。据我了解,您不能从基类定义显式或隐式运算符吗?如何将基类 Linq 结果转换为继承的 Employee 类,同时仍保持其数据库状态以便我可以提交更改?

有帮助吗?

解决方案

对于 LINQ-to-SQL,继承有两种工作方式:

  • 单个表上的鉴别器(不适合,因为您的数据不是同质的)
  • 基类/多表(并不是说 dbml 不支持这一点 - 仅当您手动编写类时)

LINQ-to-SQL 不支持多表继承(即具有来自多个表的数据的单个对象)。实体框架可以,但更复杂;你用 .Cast<T>.OfType<T> 在 EF 中根据子类型进行投射/过滤。

您可能想看看:

这里基类的目的是什么?如果它添加了行为,那么您可以编辑 dbml 为所有实体指定一个公共基类。如果有 数据属性 然后事情就变得更棘手了。

就我个人而言,我根本不会这样做......我将为不同类型保留单独的类,并正确使用数据上下文,每种类型使用单独的表:

public IEnumerable<Employee> All(int accountholder_id)
{
    return db.Employees.Where(e => e.accountholder_id == accountholder_id)
        .OrderBy(a => a.name);
}

public Employee Single(int id)
{
    return db.Employees.Single(a => a.id == id);
}

那么 - 你能澄清一下什么吗 cAccountEntity 这里有吗?

其他提示

感谢您的输入,将查看一些建议...

帐户实体背后的想法是,截至目前该网站只需要处理的员工,但在未来,他们可能希望车辆等添加到系统中,该系统被用于成本分配到一个实体,所以为了这个目的雇员的处理相同的车辆。

这个想法是,员工和车辆需要他处理同样在DB等引用,但需要对他们稍微不同的信息。这不仅是因为他们想在以后添加额外的类型复杂的设计,但不需要升级数据库...

在我的代码,但是,我想谈一个员工不是一个通用的实体类型(使控制器,查看等在MVC应用程序要容易得多)。因为你不能从基地提供用户定义的铸造到派生类我跳过inheritting它和所用的下述溶液来代替。有点繁琐,但不工作...让我知道,如果有人可以看到这样做的更好的方法。

public class cAccountEmployee
{
    private cAccountEntity entity;

    public int id
    {
        get
        {
            return this.entity.id;
        }
        set
        {
            this.entity.id = value;
        }
    }

    public string name
    {
        get
        {
            return this.entity.name;
        }
        set
        {
            this.entity.name = value;
        }
    }

    public int accountholder_id
    {
        get
        {
            return this.entity.accountholder_id;
        }
        set
        {
            this.entity.accountholder_id = value;
        }
    }

    public System.Data.Linq.EntitySet<cAccountEntityValue> cAccountEntityValues
    {
        get
        {
            return this.entity.cAccountEntityValues;
        }
    }

    public cAccountHolder cAccountHolder
    {
        get
        {
            return this.entity.cAccountHolder;
        }
    }

    public cAccountEmployee()
    {
        this.entity = new cAccountEntity();
    }

    public cAccountEmployee(cAccountEntity entity)
    {
        this.entity = entity;
    }

    public string id_number
    {
        get
        {
            try
            {
                return this.entity.cAccountEntityValues.Single(e => e.type == 1).value;
            }
            catch (Exception)
            {
                return "";
            }
        }

        set
        {
            try
            {
                this.entity.cAccountEntityValues.Single(e => e.type == 1).value = value;
            }
            catch (Exception)
            {
                this.entity.cAccountEntityValues.Add(new cAccountEntityValue()
                                            {
                                                accountentity_id = this.id,
                                                cAccountEntity = this.entity,
                                                type = 1,
                                                value = value
                                            });
            }
        }
    }
}

//In the repository
public cAccountEmployee Single(int id)
    {
        return new cAccountEmployee(db.cAccountEntities.Single(a => a.id == id));
    }
  

我怎样才能得到基类的LINQ结果投达继承Employee类

这不是一个向上转型,它是一个向下转换。

我想你不明白铸造或可能 - 例如型VS引用类型

public class Animal { }
public class Zebra : Animal { }

public class Zoo
{
    public void ShowZebraCast()
    {
        Animal a = new Animal();
        Zebra z = (Zebra)a;
    }
}

System.InvalidCastException:无法转换类型“动物”的对象为类型“斑马”

在以相同的方式,就得实体的实例,你不能垂头丧气到针对它使用一个雇员参考。

您可以转换的类型,但你必须提供一个转换方法。

public partial class Animal { }
public class Zebra : Animal { }
//in another file
public partial class Animal{
  public Zebra ToZebra(){
    return new Zebra() { //set Zebra properties here.
    };
  }
}


public class Zoo
{
    public void ShowZebraConvert()
    {
        Animal a = new Animal();
        Zebra z = a.ToZebra();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top