WebApi & EntityFramework with POCO

<h3>Create two projects</h3>

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


    Two Project

<h3>SQL Server Project</h3>

  1. 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
  2. Create a database from your SSMS(SQL Server Management Studio)


    Create Empty Database
  3. Import the Database you just created to the SQL Server Project.


    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.


    Student.sql

    Course.sql

    StudentCourse.sql
  5. Create Foreign Key for both of StudentId and CourseId columns.


    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.


    Publish

    Target Platform Version

<h3>Web API Project</h3>

  1. Create a POCO file, that actually is the <code>context</code> file.


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


    XXXContext File
  1. Insert <code><connectionString> <add ...> </connectionString></code> to the <code>.Web.config</code> file.


    <connectionString>
  2. 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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie阅读 2,914评论 0 3
  • =========================================================...
    lavor阅读 3,507评论 0 5
  • 今夜 夜色撩人 皓月 清凉如水 蝉鸣也不显的聒噪 就 那一抹光 我啊 记起天各一方的你 年少时 心口的朱砂痣 青葱...
    潋兮阅读 333评论 0 1
  • 日随天雨渐微凉, 阴雨连绵雨未下, 地里花生未收完, 落地长生果未干, 天气转凉秋高爽, 早出晚归添衣裳,
    A把时间当做朋友阅读 172评论 0 2