Note that there are some explanatory texts on larger screens.

plurals
  1. POlinq-to-sql won't serialize references
    primarykey
    data
    text
    <p>So I have a couple of tables in a linq-to-sql dbml file:</p> <p><img src="https://i.stack.imgur.com/5X7at.png" alt="my dbml layout"></p> <p>I have set up a WCF REST get endpoint to user. I cannot get it to pass back a JSON structured like this:</p> <pre><code>{ ID: '123', Username: 'fdsaf', ... Role: { ID: '123', ... } } </code></pre> <p>If I set the DBML serialization mode from "None" to "Unidirectional," I can get the user object without the role. If I leave the serialization mode on none and remove the association, I can also get the user object. As a sanity check, I can also get the role with no user. As soon as I try to include a reference, it attempts to serialize twice (in the debugger) and I get the following error after what appears to be a successful function call on the client:</p> <p><img src="https://i.stack.imgur.com/ErdZQ.png" alt="client error"></p> <p>Additionally, I <em>can</em> get what I'm looking for if I open the association and turn the child property access to internal. However, I will sometimes want to pass back roles with collections of users, so this will not be sufficient.</p> <p><img src="https://i.stack.imgur.com/08u6g.png" alt="insufficient workaround"></p> <p>I include this piece of information because it seems to indicate that the serializer is trying to serialize User > Role > User > Role ... etc., which is obviously insufficient. But there must be a middle ground between a circular and a zero-level-deep reference.</p> <p>Code below:</p> <pre><code>using (DataContext context = new DataContext()) { DataLoadOptions opts = new DataLoadOptions(); opts.LoadWith&lt;User&gt;(u =&gt; u.Role); context.LoadOptions = opts; return context.Users .Where(u =&gt; u.ID == id) .Where(u =&gt; u.Hash == hash) .FirstOrDefault(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload