Association Mappings don't get populated in queries

Sep 16, 2009 at 9:46 PM


I'm trying to use the XmlMapping or AttributeMapping to populate a clr object that has a member that belongs to another table. Ie: Customer has a property called Orders, in the Northwind database.
When I use the SqlQueryProvider with either the XmlMapping, ImplicitMapping, or AttributeMapping, only the members using built-in CLR types (int, DateTime) get populated, while the association (Orders) remains null.

I was wondering if anyone has been able to get Assocations working with the IQToolkit.

To better clarify the situation, here's an example scenario:
I have a class ComplicatedObject that has a name member and a foreign key member that references SimpleObject. How can I get a ComplicatedObject back with all members filled from the database, including SimpleObject that has it's information in another table?


Example Query:

Northwind northwind = new Northwind(new SqlQueryProvider(
    new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true"),
    XmlMapping.FromXml(TSqlLanguage.Default, File.ReadAllText("Northwind.xml"))));

var customers = from c in northwind.Customers select c;
foreach (var item in customers)
    Console.WriteLine(item); // item.Orders is always null!

Sep 16, 2009 at 11:30 PM

I needed to create my own QueryPolicy and overrde the IsIncluded class to get relations working. Returning true for the members you wish to have a relation solves the issue.

I still have issues using Linq Queries that ultimately end up with a n:n type of relationship but I believe this is a known issue. An example of this would be passing to the TestPolicy the strings "Orders", "Customer". This causes a Customer to have a list of Orders that each reference a Customer. An exception with the message, "Sequence contains more than one element."

I should be able to move forward now though. Sorry for the trouble!

Sep 17, 2009 at 3:04 AM

Yes, QueryPolicy is what you need.  There hasn't been much development of QueryPolicy yet.  Expect future releases to expand on this more.