All along, the way to use DB first, to get the table name, and finally has not been its law. Until last night, when deserializing its own assembly, it was suddenly discovered where the mapping of the EF table structure and data entity classes existed. And then there's this article.
Let's take a step-by-step.
1. Create a new entity set using EF first. In this procedure, remember the model namespace name in the following useful.
2. Open the edmx file using XML. And then we find the node
<edmx:mappings> <MappingSpace= "C-s"xmlns= "Http://schemas.microsoft.com/ado/2009/11/mapping/cs"> <entitycontainermappingStorageEntityContainer= "Reportservermodelstorecontainer"CdmEntityContainer= "Reportserverentities"> <entitysetmappingName= "Batches"> <EntityTypeMappingTypeName= "Reportservermodel.batch"> <MappingFragmentStoreEntitySet= "Batch"> <ScalarPropertyName= "BatchId"ColumnName= "BatchId" /> <ScalarPropertyName= "Addedon"ColumnName= "Addedon" /> <ScalarPropertyName= "Action"ColumnName= "Action" /> <ScalarPropertyName= "Item"ColumnName= "Item" /> <ScalarPropertyName= "Parent"ColumnName= "Parent" /> <ScalarPropertyName= "Param"ColumnName= "Param" /> <ScalarPropertyName= "Boolparam"ColumnName= "Boolparam" /> <ScalarPropertyName= "Content"ColumnName= "Content" /> <ScalarPropertyName= "Properties"ColumnName= "Properties" /> </MappingFragment> </EntityTypeMapping> </entitysetmapping> </entitycontainermapping> </Mapping> </edmx:mappings>
Where the data entity class name is on the type name of <entitytypemapping typename= "Reportservermodel.batch" >. Rules are model namespaces + entity class names.
The data table name is on the storeentityset of MappingFragment storeentityset= "Batch".
At this point, we compile and then where to find this mapping relationship file?? Please look at the back
3. After compiling, use Ilspy to open the assembly, as
The contents of this resource file are identical to what we saw in the second step. Haha, at this point, we have found the EF DB First mapping relationship where it existed after compilation. The rest is that we write code to get these. The rest will not be said.
Directly on the code
XmlDocument xmldoc =NewXmlDocument ();
This is the data that is read from the resource file.varstream = assembly.getexecutingassembly (). GetManifestResourceStream ("TEST.MSL"); Xmldoc.load (stream); Stream. Close (); Stream. Dispose (); foreach(XmlNode nodeinchxmlDoc.DocumentElement.FirstChild.ChildNodes) {//entitysetmapping if(node. NodeType! =xmlnodetype.element)Continue; //class Honour Point foreach(XmlNode typemappinginchnode. ChildNodes) {//EntityTypeMapping if(Typemapping.nodetype! =xmlnodetype.element)Continue; //take a table honour point stringTableName =""; foreach(XmlNode mappingfragmentinchtypemapping.childnodes) {if(Mappingfragment.nodetype! =xmlnodetype.element)Continue; TableName= (mappingfragment asXmlElement). GetAttribute ("StoreEntitySet"); Break; } //class name stringTypeName = (typemapping asXmlElement). GetAttribute ("TypeName"); Tablenames[typename]=TableName; Break; } }
Concrete implementation, please yourselves to achieve!!!
The logic is not complex just need to find the data entity and the table structure of the mapping relationship where we can get what we want.
EF DB first Get table name