WebApi & EntityFramework with POCO

Create two projects

  1. Creating two projects in one Solution, which including a SQL Server Project, and a Web Api Project.


    Two Project

SQL Server Project

  1. Install EntityFramework. from NuGet Package Management, and Entity Framework Reverse POCO from Extension and Updates in the menu of Tools.


    From NuGet

    From Extension and Updates
  2. Create a database from your SSMS(SQL Server Management Studio)


    WebApi & EntityFramework with POCO_第1张图片
    Create Empty Database
  3. Import the Database you just created to the SQL Server Project.


    WebApi & EntityFramework with POCO_第2张图片
    Import to SQL Server Project
  4. 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.


    WebApi & EntityFramework with POCO_第3张图片
    Student.sql

    WebApi & EntityFramework with POCO_第4张图片
    Course.sql

    WebApi & EntityFramework with POCO_第5张图片
    StudentCourse.sql
  5. Create Foreign Key for both of StudentId and CourseId columns.


    WebApi & EntityFramework with POCO_第6张图片
    ForeignKey
  6. Right-Click the SQL Server Project, and then Publish to the database, and don't forget to switch the platform version to appropriate one.


    WebApi & EntityFramework with POCO_第7张图片
    Publish

    Target Platform Version

Web API Project

  1. Create a POCO file, that actually is the context file.


    XXXContext.tt
  2. Edit configuration file from your Context file.


    WebApi & EntityFramework with POCO_第8张图片
    XXXContext File
  1. Insert to the .Web.config file.


  2. In the folder of XXXContext.cs, find the constructor method of XXXContect class, add line inside of it.
    base.Configuration.ProxyCreationEnabled = false;


    WebApi & EntityFramework with POCO_第9张图片
    Unable the ProxyCreationEnabled property

All Set!

你可能感兴趣的:(WebApi & EntityFramework with POCO)