Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ to EF abstract column names

Posted on 2010-08-31
6
Medium Priority
?
521 Views
Last Modified: 2013-11-11
Hi,
I was wondering whether it is possible to to do something like this:

            var x = (from d in DBContext.SOMETABLE
                          select d).Single();

            x.column("SOMECOLUMN") = "abc";
            DBContext.SaveChanges();

ie. I want to be able to access the column names abstractly, this is because I conditionally need to updated different columns and begin able to do the above will make the code far shorter and easier to read.
0
Comment
Question by:Alw1n
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:with
ID: 33566105
X is basically just a regular class, so you can use regular old Reflection to set a class's property by name.  Since the code-generated class EF makes should have the same column names in the database by default, it should work out well enough (psuedo-code, ymmv but should be pretty close)

PropertyInfo columnproperty = SOMETABLE.GetType().GetProperty("SOMECOLUMN");

var x = (from d in DBContext.SOMETABLE select d).Single();

columnproperty.SetValue(x, "abc", null);
0
 

Author Comment

by:Alw1n
ID: 33566465
Thanks, it looks promising but I can't access my column with:

PropertyInfo columnproperty = SOMETABLE.GetType().GetProperty("SOMECOLUMN");

It appears that the SOMECOLUMN property is buried somewhere deeper in the entity object as the code above produces a result of null.
0
 
LVL 3

Accepted Solution

by:
with earned 2000 total points
ID: 33566616
Here's a more concrete example; I actually tested this so it should work.  This is using my context, you'll have to replace the names with yours:

etsykitEntities context = new etsykitEntities();

//Get 1 item to deal with.  I'll just make one, although I could have selected one
Item oneItem = ctx.Items.CreateObject();

//Notice the difference here; getting type of Item, not Item*s*
PropertyInfo p = typeof(Item).GetProperty("Title");

//Setting the "Title" field of this entity using reflection:
p.SetValue(oneItem, "new title", null);

The problem with my initial example was that I had you use the *object set* type, but that doesn't have any database-specific properties on it.  You want to use the *entity* type.  Hope this makes more sense.
0
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 

Author Closing Comment

by:Alw1n
ID: 33568114
Worked perfectly, thank you
0
 
LVL 21

Expert Comment

by:naspinski
ID: 33568604
I wrote an article about this a while back: http://www.naspinski.net/post/Edit-an-Object-Property-Value-Dynamically-at-Runtime.aspx

Also, I included a simple extension to do this without any code in m my utilities library: http://naspinski.codeplex.com/wikipage?title=DynamicProperty&referringTitle=Documentation
0
 

Author Comment

by:Alw1n
ID: 33574161
Thanks Naspinski, that is a really cool idea to use this as an extension!
0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question