EF DB first Get table name

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.