Solved

LINQ to EF abstract column names

Posted on 2010-08-31
6
483 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 48
coded character set iptc field within a jpeg 1 71
Get Client IP on RDS - VB.NET 15 30
I need help converting a bitmap to an image in VB.Net 1 16
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now