0

I'm new to the entity framework; working on my first app using it. So far, I love it! I really like the strongly typed way of interacting with fields in the table/query in the context of EF.

However, I'm wondering if there's a way of accessing the fields of a table/query, the old fashion way, by naming the field as a string? For example, assuming that efObject is some table in the .EDMX, is there a way of doing something like this:

efObject["FirstName"].Value = "Fred";

If there is such a way of doing this, I don't know what the properties are, what the methods would be, etc.

I'm working with EF 4.2.

Rod
  • 4,107
  • 12
  • 57
  • 81
  • why when you got a strong typed access way? – Massimiliano Peluso Feb 15 '12 at 17:10
  • Good question. The issue is I've got a WCF service I wrote years ago, which returns ADO.NET datasets. The only way I know of to iterate through all of the tables it returns (and there's a lot of them, with lots of fields) is to do it like this: dataSet.Tables[n].Rows[m][o]; – Rod Feb 15 '12 at 17:19
  • (Man, I'm not used to not being able to put carriage returns into my comments.) Alternatively, I could use dataSet.Tables[n].Rows[m]["FirstName"] I do NOT want to have to type a line of code for each and every column in each table in my EF model. There's about 600 fields in all of the tables; that's a heck of a LOT of typing, that I'm trying to avoid. – Rod Feb 15 '12 at 17:23

1 Answers1

1

The EF data model definition can't be easily accessed at runtime.

Have a look at this discussion for reasons and possible workarounds: Get Database Table Name from Entity Framework MetaData

If the property name and the respective field name match, you could use reflection to deduct the mapping.

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Thank you, Dennis, for other Stack Overflow article you linked to. WOW, it doesn't look like it would be easy, and in the case the article you linked to, Rick Strahl wanted the table name. I know that, it's the fields I don't know. And I know nothing about ObjectStateEntry, or other options listed, to use them. Perhaps in this case I'd be better off just using ADO.NET datasets against the local SQL Express database I'm trying to move data into. Bummer. – Rod Feb 15 '12 at 18:00