DataContext.ExecuteQuery<T> with auto properties

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
creaseypaul
Posts: 11
Joined: Mon 14 Feb 2011 15:56

DataContext.ExecuteQuery<T> with auto properties

Post by creaseypaul » Thu 28 Jul 2011 16:45

Consider the following:

Code: Select all

var db = new MyDataContext();
var data = db.ExecuteQuery("select Hello, World from myTable");
This fails when MyModel is implemented with auto properties:

Code: Select all

public class MyModel
{
      public string Hello { get; set; }
      public string World { get; set; }
}
But works when implemented with backed properties

Code: Select all

public class MyModel
{
      private string hello;
        public string Hello
        {
            get { return hello; }
            set { hello= value; }
        }

        private string world;
        public string World
        {
            get { return world; }
            set { world= value; }
        }
}
The exception is as follows:

Code: Select all

System.InvalidOperationException was unhandled by user code
  Message="Field with name k__BackingField is not found in resultset."
  Source="Devart.Data.Linq"
  StackTrace:
       at Devart.Data.Linq.Provider.g.a(IDataRecord A_0, String A_1, DataSourceInfo A_2)
       at lambda_method(ExecutionScope , b )
       at Devart.Data.Linq.Provider.ObjectReader`1.g()
       at Devart.Data.Linq.Provider.ObjectReader`1.b()
       at Devart.Data.Linq.Provider.ObjectReader`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at com.blah.MyApp.Services.GetMyModel(String user, DateTime dateTime) in C:\blah
       at com.blah.MyApp.web.Controllers.MyModelController.ImpactToday() in C:\blah
       at lambda_method(ExecutionScope , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.c__DisplayClassd.b__a()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 
Am i doing something wrong?

Thanks

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Fri 29 Jul 2011 10:57

The problem is that the .ExecuteQuery generic method tries to fill the fields, not the public properties. For each field, it searches for the result set column with the same name as the field has; provided that the field is implicitly generated by the compiler, its name may differ from the property name (and thus from the column name).

To resolve this problem, you can either explicitly declare the storage fields, or add the Column attributes to the property declarations. Please refer to the following topic for a sample:
http://www.devart.com/forums/viewtopic.php?t=15079

Please tell us if this helps.

creaseypaul
Posts: 11
Joined: Mon 14 Feb 2011 15:56

Post by creaseypaul » Tue 02 Aug 2011 15:31

Thanks StanislavK, i assumed this was the case and it isn't really a problem other than a minor inconvenience.

I do think the documentation could be clearer though

http://www.devart.com/linqconnect/docs/ ... 5D%29.html

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Wed 03 Aug 2011 16:56

Glad to see that the problem was resolved.

Thank you for the report, we will consider improving the way this question is described in our documentation.

Post Reply