继承Linq到SQL类并转换linq查询的结果

继承Linq到SQL类并转换linq查询的结果

本文介绍了继承Linq到SQL类并转换linq查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing an application where we will need to extend a basic entity into a number of different things (eg employee, vehicle etc). The design is as such that there is a Entity table and a second table with type specific values eg an employee will have an ID Number but a vehicle will have a registration number.

我继承了在数据上下文中生成的类实体,但我有问题在我的存储库中的铸造。正确的方式是什么?

I have inherited from the class entity generated in the Data Context but am having trouble with the casting in my repository. What is the correct way of doing this?

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

}

然后在我的仓库中(不继承任何东西)

Then in my repository (does not inherit anything)

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类,同时仍然保持其数据库状态,以便我可以提交更改?

The cast fails in the single method and hence I get back null. It is my understanding you cannot define explicit or implicit operators from or to a base class? How can I get the base class Linq result to cast up to the inherited Employee class, while still maintaining its db state so I can submit changes?

推荐答案

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

With LINQ-to-SQL, there are two ways inheritance can work:


  • 因为您的数据不是同类的)

  • 基类/多表(不是这在dbml中不支持 - 只有手动写类)

LINQ-to-SQL不支持多表继承(即具有多个表中数据的单个对象)。实体框架确实,但是更复杂;您在EF中使用 .Cast< T> .OfType< T> ,根据子类型

LINQ-to-SQL does not support multi-table inheritance (i.e. a single object with data from multiple tables). Entity Framework does, but is more complex; you use .Cast<T> and .OfType<T> in EF to cast/filter based on sub-types.

您可能需要查看:


  • 进行查询(结合使用 GetTable< T>( )


  • querying based on an ID (combine with GetTable<T>())
  • specifying a base-class in the dbml

类这里?如果它添加行为,那么您可以编辑dbml以为所有实体指定一个公共基类。如果它具有数据属性,那么它会变得棘手。

What is the purpose of the base class here? If it adds behaviour, then you can edit the dbml to specify a common base-class for all your entities. If it has data properties then it gets trickier.

我个人不会这样做...我会保持分离不同类型的类,并且正确地使用数据上下文,每个类型使用单独的表:

Personally, I simply wouldn't do it this way... I would keep separate classes for the different types, and use the data-context correctly, using the separate tables per type:

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 在这里?

So - can you clarify what the cAccountEntity does here?

这篇关于继承Linq到SQL类并转换linq查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:11