Solved

LINQ to EF abstract column names

Posted on 2010-08-31
6
498 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
  • 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 500 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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