How to extract entity property-column mapping in EF
I've been searching for ages for an answer to this. Although I've seen lots of similar posts, they all seem to be answered with "You need to read the mapping data directly form the XML", but I can't see an example of how to do it.
I'm using EF 4.1, Database first and Dbcontext. I'm trying to implement an auditing process, which is done by overriding SaveChanges in my custom context. I query the state manager to pull back modified records etc. All standard stuff and lots of good examples out there.
However, I want to write to a single audit table in the database, which has the column name being changed and the table name that the column belongs to. Currently I'm just writing out the entity property name for the column record and the entity type to the table record. OK this works, but it's not strictly correct as I should be writing the mapped column/table name NOT the conceptual representation. So far all my entites use exact mapping, i.e. they are the same names as the database names. However, if a developer one day creates an entity from a new table and specifies a different name for the mapping, my auditing won't be correct. I'll have audit details of a column name for example that doesn't exist! Also, I know you can map an entity to more than one table. So this is what I want to do ideally:
At the the entity property level I want to find out the column name from the database and the table name it belongs to (including the full schema - i.e. for a table MySchema.MyTable I want to write to the audit table MySchema.MyTable NOT just MyTable). This process would safeguard the auditing process form developers editing mapping names, or creating entities from more than one table etc.
All the examples say you need to use the CSSpace dataset as this holds the relationship info from the data store (SSpace) to the Conceptual store (CSpace). But this is used internally and cannot be access through the metadataworkspace API, apparently. Instead am I supposed to read the XML for CSSpace directly? If so, where is this file (embedded in to the assembly?) and how can I read it? I would think that what I want to do is very basic and an obvious requirement, yet it seems sooooo difficult to do.
Any help greatly appreciated!
If I understand your question correctly, the XML for the model is the model file - If you right click on the model and choose "Open With - XML editor", you can see it.