简体   繁体   English

在使用带有实体框架的Table-per-Type时,如何仅获取基表行?

[英]How do I get just the base table rows when using Table-per-Type with Entity Framework?

I have the following tables mapped in Entity Framework 4.2 as table-per-type. 我有以下表格在实体框架4.2中映射为table-per-type。

Gear (ID, Name, Description) // base table
Weapon (ID, Damage, Bonus) // FK to Gear table.
Armor (ID, Kinetic, Energy) // FK to Gear table.

I can get the weapons and gear separately as such: 我可以单独拿到武器和装备:

var weapons = db.Gear.OfType<Weapon>(); // Gets just weapons.
var armor = db.Gear.OfType<Armor>(); // Gets just armor.

but I can't figure out how to get just the base rows. 但我无法弄清楚如何获得基本行。 I was hoping something like this would work but it still gets all of them. 我希望这样的东西可以工作,但它仍然可以得到所有这些。

var basicGear = db.Gear.OfType<Gear>();

尝试

var basicGear = db.Gear.Where(g => !(g is Weapon || g is Armor));

Since you want all the rows that aren't specifically Weapons or Armour. 因为你想要的所有行都不是专门的武器或盔甲。 Generally, I would say you could try using the Except method . 一般来说,我会说你可以尝试使用Except方法

var weapons = db.Gear.OfType<Weapon>(); // Gets just weapons.
var armor = db.Gear.OfType<Armor>(); // Gets just armor.
var allOtherStuff = db.Gear.Except(weapons).Except(armor);

Or even 甚至

var allOtherStuff = db.Gear.Except(weapons.Union(armor));

However, I'm not aware if Except has a translation to SQL within the Entity Framework (based on some answers on here they suggest it might not but this MSDN page would suggest it might). 但是,我不知道Except是否在实体框架内有一个SQL转换(基于这里的一些答案 ,他们建议它可能不会,但这个MSDN页面会暗示它可能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码First Table-Per-Type继承包括基本类型 - Entity Framework Code First Table-Per-Type Inheritance include base type 实体框架每个类型的表:1:0…*与派生类的FK关系-是基类还是派生类的FK? - Entity Framework table-per-type: 1:0…* FK relationship with a derived class - is FK of base class or derived class? 实体框架6个自定义键,用于每种类型的表继承 - Entity framework 6 custom keys for table-per-type inheritance 实体框架流利的按表类型性能替代 - Entity Framework Fluent, Table-Per-Type performance alternatives 每种类型的表代码优先-如何区分基本类型是子类型 - Table-per-Type Code First - How to differentiate if Base type is sub type 将EF CodeFirst Base类转换为Inherited类(使用table-per-type) - Converting a EF CodeFirst Base class to a Inherited class (using table-per-type) 每个类型的表继承插入问题 - Table-per-type inheritance insert problem 每个类型的表继承插入问题 - Table-per-type inheritance insert problem 当派生表使用其自己的主键时,如何在实体框架中按类型继承设置表? - How to setup table per type inhertiance in entity framework when the derived table uses its own primary key? EF Table-Per-Type:将新的子代添加到现有父代 - EF Table-Per-Type: Add new child to an existing parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM