1

I've been able to obtain the column names from an entity using the answer for the query at Entity Framework - getting a table's column names as a string array (thanks dav_i!).

My array is obtained from

var colNames = typeof(User).GetProperties().Select(a => a.Name).ToArray();

My question is, how do I use this array of column names to specify particular values in the DbPropertyValues collection of my entity?

I have used the following C# code to previously attempt to work through the collection -

DbPropertyValues values = dbContext.Entry(item).CurrentValues;
foreach (var propName in values.PropertyNames)
{
    ...

However, what I really need is to be able to specify something like this -

for(int i = 5; i < 11; i++)
{
    var colValue = values(colNames[i]);

I want to use the array index to help select particular entity property values. Should I be using something other than DbPropertyValues?

This was so easy with ADO.Net, using DataRow[i]....

Can anyone help?

Community
  • 1
  • 1
  • If you know what column name you want to set in advance, why not just do that? For example, var user = new User(); user.Name = "someName";. The idea of the EntityFramework is to get away from magic numbers for rows and such and instead work with the objects by name. What problem are you trying to solve? – m.casey Jul 22 '14 at 20:52
  • 1
    You're not forced to use EF end to end. EF is good for standard ORM but if you have other needs you should use another tool. You can access the underlying ADO.Net infrastructure from your `DBContext`: `context.Database.Connection`; from here you can obtain the good old `DataSet`/`DataTable` stuff... – Pragmateek Jul 22 '14 at 20:55
  • @Pragmateek - didn't know about that, will give it a try. – user2517555 Jul 23 '14 at 07:59
  • @m.casey - I'd rather not have to specify individually each of say, 25 columns, if I can use a for..loop to specify columns[20] to columns[45]. It just seems more efficient? My problem is I'm working with Db tables with a lot of columns (50+) and the data I need is in contiguous blocks of columns interspersed through the whole. Ordinarily, I would specify the required entity property value as you suggest but with the numbers involved here that's just inviting errors. – user2517555 Jul 23 '14 at 07:59
  • Given your use case, I would still map the properties by name via your User object's constructor and back that with unit tests. I can supply an answer to that end if you wish. Alternatively, I'd handle it in a stored procedure the result of which can be mapped to a User object via EntityFramework. If all else fails, I'd fallback to the old DataSet/DataTable objects. While what you want is entirely possible, but it raises the hairs on the back of my neck. :) – m.casey Jul 23 '14 at 13:16
  • @m.casey - thanks for the very kind offer. I can appreciate your reservations over this, but for purely pragmatic reasons, I've decided to go for the DataSet/DataTable fallback - have already used up a couple of weeks of research and trial and error on this and need to move on to the complicated stuff... :) – user2517555 Jul 24 '14 at 11:25

0 Answers0