inheritanceLinq to SQL类并转换linq查询的结果

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

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

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

}

然后在我的存储库中(不inheritance任何东西)

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

转换在单个方法中失败,因此我返回null。 我的理解是你不能从基类定义显式或隐式运算符? 如何将基类Linq结果转换为inheritance的Employee类,同时仍保持其db状态,以便我可以提交更改?

使用LINQ-to-SQL,inheritance可以使用两种方式:

  • 单个表上的鉴别器(不合适,因为您的数据不是同质的)
  • 基类/多表(不是dbml不支持这种情况 – 只有手动编写类)

LINQ-to-SQL不支持多表inheritance(即具有来自多个表的数据的单个对象)。 entity framework确实如此,但更复杂; 在EF中使用.Cast.OfType来根据子类型进行转换/过滤。

你可能想看看:

  • 基于ID查询 (与GetTable()
  • 在dbml中指定基类

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

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

 public IEnumerable 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应用程序中使控制器,视图等更容易)。 由于您无法提供从基类到派生类的用户定义的转换,我已跳过inheritance它并使用以下解决方案。 有点麻烦,但确实有用……如果有人能看到更好的方法,请告诉我。

 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 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结果转换为inheritance的Employee类

这不是一个向上,它是一个沮丧。

我认为你不理解转换或可能 – 实例类型与引用类型。

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

System.InvalidCastException:无法将类型为“Animal”的对象强制转换为“Zebra”。

以同样的方式,您有一个实体实例,您无法向下转换以使用针对它的Employee引用。

您可以转换类型,但是您必须提供转换方法。

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