Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomapper: how to ignore a navigation property in EF (VB.NET)
    text
    copied!<p>I'm new to EF and to AutoMapper, and I'm trying to use it to create a web service. If I try to serialize EF entities directly, the navigation properties throw errors because they can't be serialized. I'd like to find a way to suppress them, and I'm told the best way to do this is to create POCOs to use as Data Transfer Objects. </p> <p>(At this point I'm thinking I might as well write my own DTOs using ADO.NET - isn't EF supposed to save me the trouble of writing my own classes?)</p> <p>I've had AutoMapper recommended to me as a way to automatically map EF entities to POCOs, so I tried using it in the following procedure, which loads the POCO:</p> <pre><code>Public Sub Load(ByVal lngUserId As Long) Dim query = From u In db.Users Where u.UserID = lngUserId Select u AutoMapper.Mapper.Map(query.FirstOrDefault(), Me) End Sub </code></pre> <p>Problem is, it comes up with an error that turns out to be related to the navigation property again, which it appears is not automatically ignored by AutoMapper.</p> <p>So, I've tried adding this line to force AutoMapper to ignore the nav property:</p> <pre><code>Dim oMap = Mapper.CreateMap(Of User, UserDto).ForSourceMember(Sub(src) src.tblUserFarms, Sub(opt) opt.Ignore()) </code></pre> <p>(tblUserFarms is the nav property)</p> <p>I converted the syntax for this from a C# example I found, but it appears that the first parameter of .ForSourceMember isn't supposed to be a Lambda expression, and I can't find a good example of how it should be used.</p> <p>Am I using the right approach, or should I be trying something else altogether? Should I simply delete all my nav properties in order to work around the problem, and lose their benefits? Is EF even the right tool for the job?</p>
 

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