【发布时间】:2012-08-13 09:15:53
【问题描述】:
我在这样的数据库中有一个简单的父子表
CREATE TABLE [Parent](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](256) NOT NULL)
ALTER TABLE [Parent] ADD CONSTRAINT [PK_Parent_Id] PRIMARY KEY ([Id])
CREATE TABLE [Child](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ParentId] [int] NOT NULL,
[Name] [nvarchar](256) NOT NULL)
ALTER TABLE [Child] ADD CONSTRAINT [PK_Child_Id] PRIMARY KEY ([Id])
ALTER TABLE [Child] ADD CONSTRAINT [FK_Child_Parent_ID]
FOREIGN KEY([ParentId]) REFERENCES [Parent] ([Id])
我在其中的数据是
父表
Id Name
1 John
子表
Id ParentId Name
1 1 Mike
2 1 Jake
3 1 Sue
4 1 Liz
这些表使用 Visual Studio 中的 Linq-2-SQL 设计器映射到 Parent 和 Child C# 对象,没有非标准选项。
我做了一个简单的测试程序来查询所有孩子和他们的父母
public partial class Parent
{
static int counter = 0;
//default OnCreated created by the linq to sql designer
partial void OnCreated()
{
Console.WriteLine(string.Format("CreatedParent {0} hashcode={1}",
++counter , GetHashCode()));
}
}
class Program
{
static void Main(string[] args)
{
using (var db = new SimpleDbDataContext())
{
DataLoadOptions opts = new DataLoadOptions();
opts.LoadWith<Child>(c => c.Parent);
db.LoadOptions = opts;
var allChildren = db.Childs.ToArray();
foreach (var child in allChildren)
{
Console.WriteLine(string.Format("Parent name={0} hashcode={1}",
child.Parent.Name, child.Parent.GetHashCode()));
}
}
}
}
上述程序的输出是
CreatedParent 1 hashcode=53937671
CreatedParent 2 hashcode=9874138
CreatedParent 3 hashcode=2186493
CreatedParent 4 hashcode=22537358
Parent name=John hashcode=53937671
Parent name=John hashcode=53937671
Parent name=John hashcode=53937671
Parent name=John hashcode=53937671
如您所见,为数据库中的每个 Child 创建了一个 Parent 对象,但最终被丢弃。
问题:
- 为什么 Linq-2-Sql 会创建这些不必要的额外
Parent对象? - 是否有任何选项可以避免创建额外的
Parent对象?
【问题讨论】:
-
它不会像您期望的那样创建单例对象,也不应该这样做。如果您有一个查询选择了一个对象,并且您开始在同一个 id 上编写更新查询,会发生什么?您是否应该在提交更改之前立即看到更改的结果?为每个查询创建一个唯一的实例。
-
@JeffMercado,不:L2S 只为每个表和键分配一个实例。这是 ORM(身份映射)的一个重要特性。
标签: c# .net linq linq-to-sql orm