"Mvc+ef"--Creating the ADO Entity Data Model and Object relational mapping with Dbfirst

Source: Internet
Author: User

Reproduced in: http://blog.csdn.net/zhoukun1008/article/details/50528145

Now more and more like MVC, not only because Itoo used him, but because it contains a lot of knowledge points let us go to learn, our Itoo on the server with EF and WCF, and for MVC, he can also work with EF to operate the database, You can also display data to users like WebForm. Below I summarize the use of Dbfirst to create the entity Database model.

1. New ASP. NET MVC 4 WEB Application

2. Create an empty template

3. Create the ADO Entity Data Model (right-click on the model file and add new item---Data-->ado. NET Entity Data Model)

4. Select the EF designer from the data (Dbfirst is selected here)

5. Click New Connection to create a new data connection.

6, fill in the server name (can be your own IP, you can also be your own user name, I use "." To replace, on behalf of the local)

7. Continue to click Next to select the version of the Entity Framework.

8. Import the appropriate table "views, stored procedures, and functions", I have only imported tables here. Click Finish to be OK.

9. Object Relationship Mapping

After the above several operations are complete, right-click the edmx file and select Open with the XML (text) editor to open it. In this way, we can see the data in the tables in the database and the entity classes in C #, and how they are mapped!

take a look at the main composition of this XML file!

The first is the data from the Blogarticlecate table in the database

  1. <EntityType name="Blogarticlecate">
  2. <Key>
  3. <propertyref name="Id" />
  4. </Key>
  5.          < property name= "Id"  < span class= "attribute" >type= "int"   Storegeneratedpattern= "Identity"  nullable= "false"  />  
  6. <property name="Author" type="int" nullable="false" />
  7. <property name="Name" type="nvarchar" maxlength=" nullable= " "False" />
  8. <property name="Remark" type="nvarchar" maxlength="$" />
  9. <property name="Statu" type="int" nullable="false" />
  10. <property name="Isdel" type="bit" nullable="false" />
  11. <property name="Addtime" type="datetime" nullable="false" />
  12. </EntityType>

Data for Blogarticlecate entity classes in CSharp

  1. <EntityType name="Blogarticlecate">
  2. <Key>
  3. <propertyref name="Id" />
  4. </Key>
  5. <property name="Id" type="Int32" nullable="false" annotation: storegeneratedpattern="Identity" />
  6.        <Property name= "Author"  type=" Int32 " nullable=" false " < span class= "tag" >/>  
  7.        < Property name= "Name"   Type= "String"  maxlength= " fixedlength=" False " unicode=" true "  Nullable=/>  
  8.        <Property name= "Remark"  type=" String " maxlength="  < " span class= "attribute" >fixedlength= "false"  unicode=< Span class= "Attribute-value" > "true"  />  
  9. <property name="Statu" type="Int32" nullable="false" />
  10. <property name="Isdel" type="Boolean" nullable="false" />
  11.        < Property name= "Addtime"   type= "DateTime"  nullable=< Span class= "Attribute-value" > "false"  precision= "3"  />  
  12.        <NavigationProperty  name= "blogarticles"   Relationship= "self.fk_blogarticle_blogarticlecate"   Fromrole= "blogarticlecate"  torole=" blogarticle " />  
  13. <navigationproperty name="Bloguser" relationship="Self.fk_blogarticlecate_bloguser" fromrole= "blogarticlecate" torole="Bloguser" />
  14. </EntityType>

Correspondence between tables in a database and entities

  1. <entitysetmapping name="blogarticlecates">
  2. <entitytypemapping typename="Oumindblogmodel.blogarticlecate">
  3. <mappingfragment storeentityset="Blogarticlecate">
  4. <scalarproperty name="id" columnname="id" />
  5. <scalarproperty name="Author" columnname="Author" />
  6. <scalarproperty name="name" columnname="name" />
  7. <scalarproperty name="Remark" columnname="Remark" />
  8. <scalarproperty name="Statu" columnname="Statu" />
  9. <scalarproperty name="Isdel" columnname="Isdel" />
  10. <scalarproperty name="Addtime" columnname="addtime" />
  11. </mappingfragment>
  12. </entitytypemapping>
  13. </entitysetmapping>

looking at the corresponding relationship above, the attributes in an entity class should correspond to a field in the database table, as well as their respective data types in the C # class and in the table. It was with the one by one correspondence above that I realized that when we used EF to manipulate the data in the entity class, why did EF map the data from the "attributes" in the entity to the "fields" in the table.

"Mvc+ef"--Creating the ADO Entity Data Model and Object relational mapping with Dbfirst

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.