Troubles with DLINQ
I'm having troubles with DLINQ.
t_parent table
parent_id parent_name
1 Avalon
2 Indigo
3 Whidbey
4 Yukon
t_child table
child_id parent_id child_name
1 1 Chris Anderson
2 1 Nate Dunlap
3 2 Don Box
4 3 Daniel Fernandez
5 4 Euan Garden
I've not used SQLMETAL tool to generate C# classes from my database. I've developed by self me.
| |
using System; using System.Collections.Generic; using System.Text; using System.Data.DLinq;namespace LINQSample { [Table(Name="t_parent")] publicclass Parent { [Column(Name="parent_id",Id=true)] public Int32 Id; [Column(Name="parent_name")] public String Name; private EntitySet<Child> _childs; [Association(Storage="_childs", OtherKey="ParentID")] public EntitySet<Child> Childs { get {returnthis._childs; } set {this._childs = value; } } } [Table(Name="t_child")] publicclass Child { [Column(Name="child_id",Id=true)] public Int32 Id; [Column(Name="parent_id")] public Int32 ParentID; [Column(Name="child_name")] public String Name; private EntityRef<Parent> _parent; [Association(Storage="_parent", ThisKey="ParentID")] public Parent Parent { get {returnthis._parent.Entity; } set {this._parent.Entity = value; } } } }
|
My application console code is:
| | using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Query; using System.Xml.XLinq; using System.Data.DLinq;namespace LINQSample { class Program { staticvoid Main(string[] args) { DataContext db =new DataContext( "server=someserver;database=sample;user id=somebody;password=XXX;" ); db.Log = Console.Out; Table<Parent> Parents = db.GetTable<Parent>(); var q = from pin Parents select p; ObjectDumper.Write( q, 1 ); Console.Read(); } } }
|
Quick Console:
SELECT [t0].[parent_id], [t0].[parent_name] AS [Name]
FROM [t_parent] AS [t0]
Childs=... Id=1 Name=Avalon
SELECT [t0].[child_id] AS [Id], [t0].[child_name] AS [Name], [t0].[parent_id]
FROM [t_child] AS [t0]
WHERE [t0].[parent_id] = 1
Childs: Parent={ } Id=1 ParentID=1 Name=Chris Anderson
Childs: Parent={ } Id=2 ParentID=1 Name=Nate Dunlap
Childs=... Id=2 Name=Indigo
Childs: Parent={ } Id=2 ParentID=1 Name=Nate Dunlap
Childs=... Id=3 Name=Whidbey
SELECT [t0].[child_id] AS [Id], [t0].[child_name] AS [Name], [t0].[parent_id]
FROM [t_child] AS [t0]
WHERE [t0].[parent_id] = 3
Childs: Parent={ } Id=4 ParentID=3 Name=Daniel Fernandez
Childs=... Id=4 Name=Yukon
Childs: Parent={ } Id=4 ParentID=3 Name=Daniel Fernandez
What s up?
Javier Luna
--http://guydotnetxmlwebservices.blogspot.com/
You should look Quick Console results.
/* execute sql query to fill Parent */
Childs=... Id=1 Name=Avalon
/* execute sql query to fill Child (parent_id = 1) */
Childs: Parent={ } Id=1 ParentID=1 Name=Chris Anderson
Childs: Parent={ } Id=2 ParentID=1 Name=Nate Dunlap
Childs=... Id=2 Name=Indigo
/* SHOULD execute sql query to fill Child (parent_id = 2) */
Childs: Parent={ } Id=2 ParentID=1 Name=Nate Dunlap
Childs=... Id=3 Name=Whidbey
/* execute sql query to fill Child (parent_id = 3) */
Childs: Parent={ } Id=4 ParentID=3 Name=Daniel Fernandez
Childs=... Id=4 Name=Yukon
/* SHOULD execute sql query to fill Child (parent_id = 4) */
Childs: Parent={ } Id=4 ParentID=3 Name=Daniel Fernandez
This should be ideal results
Childs=... Id=1 Name=Avalon
Childs: Parent={ } Id=1 ParentID=1 Name=Chris Anderson
Childs: Parent={ } Id=2 ParentID=1 Name=Nate Dunlap
Childs=... Id=2 Name=Indigo
Childs: Parent={ } Id=3 ParentID=2 Name=Don Box
Childs=... Id=3 Name=Whidbey
Childs: Parent={ } Id=4 ParentID=3 Name=Daniel Fernandez
Childs=... Id=4 Name=Yukon
Childs: Parent={ } Id=5 ParentID=4 Name=Euan Garden
Cheers,
Javier Luna
-- http://guydotnetxmlwebservices.blogspot.com/
Right, but obviously it doesn't. I rewrote your code a bit to this.
| |
var q = from p in Parents select p; foreach (var obj in q) { Console.WriteLine("{1} = {0}",obj.Id,obj.Name); foreach(var c in obj.Childs) Console.WriteLine("\t{0} {1}",c.Id,c.Name); }
|
Saw the same results.
Fired up profiler to see what the heck is going on. Here's the queries that actually got shipped to SQL Server:
SELECT [t0].[parent_id], [t0].[parent_name] AS [Name]
FROM [t_parent] AS [t0]
SELECT [t0].[child_id] AS [Id], [t0].[child_name] AS [Name], [t0].[parent_id]
FROM [t_child] AS [t0]
WHERE [t0].[parent_id] = 1
SELECT [t0].[child_id] AS [Id], [t0].[child_name] AS [Name], [t0].[parent_id]
FROM [t_child] AS [t0]
WHERE [t0].[parent_id] = 3
Notice the lack of two and four. This smells like a bug to me. I can report it as such if you like.