Asked by:
entity framework acces by string name

Question
-
User-1982615125 posted
hi.
how do i acces an model's column name by string?
such as
myrowmodel.column("id)) = "465746"
myrowmodel.column("food)) = "fish"
and how do i convert it back to string?
dim str as string = myrowmodel.id.tostring
Saturday, January 20, 2018 3:09 PM
All replies
-
User-1664597257 posted
What is myrowmodel? If you want answers you need to be more specific when you ask questions and post meaningful code samples
Saturday, January 20, 2018 6:27 PM -
User-1982615125 posted
i havent been specific, because i wanted everything to be up to you. im open to use any scenario thats efficient to do this:
Myrowmodel is a class..
normally in Entity framework, you say MyrowModel.ID = "5678"
but i want to acces the ID property trough string. such as Myrowmodel.property("ID") = "5678"
Sunday, January 21, 2018 2:21 AM -
User-1664597257 posted
Dunno why you would like to do something like that but one way is using reflection:
public static object GetPropValue(object src, string propName) { return src.GetType().GetProperty(propName).GetValue(src, null); }
You could turn this into an extension method if you wantSunday, January 21, 2018 10:21 AM -
User475983607 posted
i havent been specific, because i wanted everything to be up to you. im open to use any scenario thats efficient to do this:
Myrowmodel is a class..
normally in Entity framework, you say MyrowModel.ID = "5678"
but i want to acces the ID property trough string. such as Myrowmodel.property("ID") = "5678"
This requirement is unusual at best. Can you explain the problem you are trying to solve? Why do you want to force a strongly typed model to be weakly typed? Especially when your previous posts criticize weak typing. Very confusing...
Sunday, January 21, 2018 12:49 PM -
User-1982615125 posted
i have two occurences to need this:
1) the column needed to set or read is beign dynamically calculated. f.e : ("Column_adres_" & variable)
2) i need to reference acces trough usercontrols. f.e : 'dim t as new usercontrol("Column_1")'
Sunday, January 21, 2018 5:42 PM -
User475983607 posted
i have two occurences to need this:
1) the column needed to set or read is beign dynamically calculated. f.e : ("Column_adres_" & variable)
2) i need to reference acces trough usercontrols. f.e : 'dim t as new usercontrol("Column_1")'
Entity Framework is strongly typed. You can learn how to use Entity Framework in a Web Forms application by going through the Getting Started tutorials.
Sunday, January 21, 2018 11:03 PM -
User-1664597257 posted
It looks to me that you are applying RDBMS logic to your model. Entity Framework is ORM (Object Relational Mapper) - when you look at your code you have to forget about database columns and rows. You are dealing with objects and collection of objects. Retrieve the object, update it and save the state and that is what strong typing is for.
I would suggest you look into your application and redesign it along these lines.
Monday, January 22, 2018 6:37 AM -
User-1982615125 posted
i obviously know that..
i have a hackish reason to want to do that. because i want to dynamically calculate the destination/source object, or reference to it (store/pass its name). you cant do everything with strong typed names at runtime, which string names can. for example i can detach a number from a column name, do a calculation with it, and then acces another column with another number in it.
you also missed another message of mine:
i have two occurences to need this:
1) the column needed to set or read is beign dynamically calculated. f.e : ("Column_adres_" & variable)
2) i need to reference acces trough usercontrols. f.e : 'dim t as new usercontrol("Column_1")'
Monday, January 22, 2018 2:31 PM -
User753101303 posted
Hi,
Another option could be to expose a collection. This if because you have 2 or 3 address lines? For the user control you might be able to take advantage of lambda expression to have something such as :
Dim t as new UserControl(Of YourModel)(function(o) o.Column_1)
And then your user contorl should be able to read/write this property for binded object. if using 4.5 you could also have a look at maybe at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data
Monday, January 22, 2018 2:43 PM -
User475983607 posted
As far as I can tell, the problem you are trying to solve is has to do with HTML forms and how to bind the forms collection to an object. This is generally accomplished by passing the forms collection to a helper (a factory) that populates the objects/collections. In modern frameworks like MVC a model binder performs this function during the request.
Again, the link I provided explains how to do model binding in Web Forms.
Monday, January 22, 2018 2:50 PM -
User-1982615125 posted
no it has nothing to do with that. dont think too complicated. just review the information i provided, and thats what you need. anything you com eup with, is definetely unrelated.
Monday, January 22, 2018 3:05 PM