2

这是一个有趣的问题。假设我们在数据库中有相关的表,例如 Instrument 和 Currency。Instrument 表有一个 currency_id 字段,该字段映射到 Currency 表中的条目。在 Linq 土地上有什么更好的方法:

a) 在 DataContext 中创建 Instrument 和 Currency 实体,然后创建关联或简单地在 Linq 查询中使用 join 或

b)在数据库中创建一个视图,将 Instrument 和 Currency 连接起来(从而将 currency_id 解析为货币代码)并将其用作 Linq 上下文中的实体?

4

4 回答 4

1

你会独立使用它们吗?如果是这样,您将需要为每个将独立使用的实体。我怀疑您将独立使用货币(例如,允许您在创建工具时选择货币的下拉菜单)。既然如此,我认为将它们分开并建立关联会更容易。

于 2009-01-05T19:04:45.653 回答
0
  • If you load in Instrument and later use the Currencies property to load in related Currencies, there will be two queries.

  • If you issue a linq query with a join, linq will convert that to sql with a join and you get all the data at once.

  • If you set up a DataLoadOptions, you get all the data in one query and you do not have to write the join.

http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.loadwith.aspx

DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Instrument>(i => i.Currencies)
myDataContext.LoadOptions = dlo;
于 2009-01-06T14:43:57.613 回答
0

随着 ORM 现在抽象了数据访问逻辑,不再需要特定的视图功能。最好将其留给 ORM,因为这是其功能的一部分。

然而,视图对于简化存储过程代码甚至对于创建有用的索引可能仍然有用。

于 2009-01-05T19:17:31.930 回答
-1

我发现 LINQ 是喜怒无常的。我可以间隔 1 分钟运行相同的查询并得到不同的结果。注意我正在使用本地数据库,所以我知道数据没有改变。在我的选择中,使用带有数据集的视图要可靠得多,尤其是在连接时。

于 2018-11-30T04:54:39.853 回答