我有一个简单的亲子数据库表中,像这样为什么linq-2-sql会创建额外的不必要的对象?
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
这些表映射到Parent
和Child
使用Visual Studio中的Linq-2-SQL设计器的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
正如你可以看到一个Parent
目标是为每一个Child
创建只在数据库中被丢弃。
问题:
- 为什么LINQ的-2-SQL创建这些不必要的额外
Parent
对象? - 是否有任何选项可以避免创建额外的
Parent
对象?
它不会像您所期望的那样创建单例对象,也不应该这样做。如果您有一个查询选择了一个对象,并且您开始在同一个ID上编写更新查询,会发生什么?您应该在提交更改之前立即看到更改的结果吗?为每个查询创建一个唯一的实例。 – 2012-08-17 00:09:25
@JeffMercado,no:L2S只发布每个表和关键字的一个实例。这是ORM(身份地图)的一个重要特征。 – usr 2012-08-20 21:11:20