Column Sequence and the Entity Framework
So, when you bind a collection of Entity objects to a grid component, the grid displays those fields in the sequential order they are found in the SQL Table they came from. This shows that the ordinal position of the fields are associated with t开发者_如何学Pythonheir corresponding entity properties... somehow or other.
So here is the question: How can I obtain the table field ordinal position by reflecting over an Entity Framework Entity's properties?
Let me tell you what I know and what I have tried. Apparently, each data field property in an EF Entity is decorated with a System.Runtime.Serialization.DataMemberAttribute. This attribute appears to have an Order property. However, I have discovered that this property does not contain what I am looking for. The value for all Data Properties in an Entity seems to be -1. What ever order is, it isn't ordinal position.
Has anybody dealt with this?
Relational data attributes have no order nor ordinal. Columns in a table have no order, nor ordinal (physical implementation asside). Result set column positions correspond to specific queries, based on the projected list of the query, and the application always knows the ordinal because it knows the projection list it asked for.
If you want to display columns in a certain order in a grid, display them in the order you desire. If you want to retrieve a column ordinal number in the physical storage for ad-hoc queries not known at development time, then you're going to interogate the physical storage directly using some storage specific mechanism. For example, if the storage is SQL Server you would look in sys.columns
.
I know it has been a while since you've asked the question but I thought I'd present this anyway. I've recently created a generic class for editing my EF objects and in doing so needed to iterate the properties associated with a given type. I'm not sure exactly what you were trying to accomplish but here is what I have:
Dim Id as Integer = 1
pEditRecord = (From locs In BIPContext.AspNetUsers Where locs.Id = Id Select locs).FirstOrDefault()
Dim iProps = pEditRecord.GetType().GetProperties()
For z As Integer = 0 To iProps.Count - 1
Dim iColName = iProps(z).Name
Dim iVal = iProps(z).GetValue(pEditRecord)
Next
How can I obtain the table field ordinal position by reflecting over an Entity Framework Entity's properties?
How can I get the name of the cook of some food by looking at the food?
You can not.
YOu could ask the EF metadata - not the class - but that would mean nothing as the metadata can have another order than the field in the database.
If you want the field in the database, get the table name, then query INFORMATION_SCHEMA and get the field ordinals from there (sure they are there), but anything you do with entity framework can have a totally different order than the storage in the database.
精彩评论