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
- <EntityType name="Blogarticlecate">
- <Key>
- <propertyref name="Id" />
- </Key>
- < property name= "Id" < span class= "attribute" >type= "int" Storegeneratedpattern= "Identity" nullable= "false" />
- <property name="Author" type="int" nullable="false" />
- <property name="Name" type="nvarchar" maxlength=" nullable= " "False" />
- <property name="Remark" type="nvarchar" maxlength="$" />
- <property name="Statu" type="int" nullable="false" />
- <property name="Isdel" type="bit" nullable="false" />
- <property name="Addtime" type="datetime" nullable="false" />
- </EntityType>
Data for Blogarticlecate entity classes in CSharp
- <EntityType name="Blogarticlecate">
- <Key>
- <propertyref name="Id" />
- </Key>
- <property name="Id" type="Int32" nullable="false" annotation: storegeneratedpattern="Identity" />
- <Property name= "Author" type=" Int32 " nullable=" false " < span class= "tag" >/>
- < Property name= "Name" Type= "String" maxlength= " fixedlength=" False " unicode=" true " Nullable=/>
- <Property name= "Remark" type=" String " maxlength=" < " span class= "attribute" >fixedlength= "false" unicode=< Span class= "Attribute-value" > "true" />
- <property name="Statu" type="Int32" nullable="false" />
- <property name="Isdel" type="Boolean" nullable="false" />
- < Property name= "Addtime" type= "DateTime" nullable=< Span class= "Attribute-value" > "false" precision= "3" />
- <NavigationProperty name= "blogarticles" Relationship= "self.fk_blogarticle_blogarticlecate" Fromrole= "blogarticlecate" torole=" blogarticle " />
- <navigationproperty name="Bloguser" relationship="Self.fk_blogarticlecate_bloguser" fromrole= "blogarticlecate" torole="Bloguser" />
- </EntityType>
Correspondence between tables in a database and entities
- <entitysetmapping name="blogarticlecates">
- <entitytypemapping typename="Oumindblogmodel.blogarticlecate">
- <mappingfragment storeentityset="Blogarticlecate">
- <scalarproperty name="id" columnname="id" />
- <scalarproperty name="Author" columnname="Author" />
- <scalarproperty name="name" columnname="name" />
- <scalarproperty name="Remark" columnname="Remark" />
- <scalarproperty name="Statu" columnname="Statu" />
- <scalarproperty name="Isdel" columnname="Isdel" />
- <scalarproperty name="Addtime" columnname="addtime" />
- </mappingfragment>
- </entitytypemapping>
- </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