<h3>Create two projects</h3>
-
Creating two projects in one Solution, which including a SQL Server Project, and a Web Api Project.
Two Project
<h3>SQL Server Project</h3>
-
Install <code>EntityFramework</code>. from NuGet Package Management, and <code>Entity Framework Reverse POCO</code> from Extension and Updates in the menu of Tools.
From NuGet
From Extension and Updates -
Create a database from your SSMS(SQL Server Management Studio)
Create Empty Database -
Import the Database you just created to the SQL Server Project.
Import to SQL Server Project -
Create three tables, Student.sql, Course.sql, and StudentCourse.sql. Because student to course is the relationship of many-to-many. Here we should add a cross table between them, that is StudentCourse.sql.
Student.sql
Course.sql
StudentCourse.sql -
Create Foreign Key for both of StudentId and CourseId columns.
ForeignKey -
Right-Click the SQL Server Project, and then Publish to the database, and don't forget to switch the platform version to appropriate one.
Publish
Target Platform Version
<h3>Web API Project</h3>
-
Create a POCO file, that actually is the <code>context</code> file.
XXXContext.tt -
Edit configuration file from your Context file.
XXXContext File
-
Insert <code><connectionString> <add ...> </connectionString></code> to the <code>.Web.config</code> file.
<connectionString> -
In the folder of XXXContext.cs, find the constructor method of XXXContect class, add line inside of it.
<code>base.Configuration.ProxyCreationEnabled = false;</code>
Unable the ProxyCreationEnabled property
<h3>All Set!</h3>
网友评论