Solved

LINQ to EF abstract column names

Posted on 2010-08-31
6
478 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 to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

22 Experts available now in Live!

Get 1:1 Help Now