简体   繁体   English

将Web API连接到SQL Server

[英]Connecting Web API to SQL Server

I am new to ASP.NET and I am creating a Web API using sort of code first. 我是ASP.NET的新手,我首先使用某种代码来创建Web API。 I have model class call gender and defined as follow 我有模型类调用性别,定义如下

public class Gender
{
    public int Id { get; set; }
    public string Description { get; set; }
}

I decided to create a new folder call DBContext and inside will defined all my DBContext , so for the gender class I have created GenderDb and look like follow: 我决定创建一个名为DBContext的新文件夹,并在其中定义所有DBContext ,因此对于性别类,我创建了GenderDb ,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace BackAPI.Models
{
    public class GenderDB : DbContext
    {
       // not define yet
    }
}

However I am having an issue however with DbContext not being defined, apparently I am supposed to use Entity Framework, however I want to create my database not through Visual Studio, but using SQL Server 2014 Express. 但是,我遇到了一个问题,但是DbContext ,显然我应该使用实体框架,但是我不想通过Visual Studio,而是使用SQL Server 2014 Express创建数据库。

I did add my data connection and can see that I created a table in SQL Server, however how do I fix DbContext , if I use EF wouldn't that just create it locally and that not what I want 我确实添加了数据连接,并且可以看到我在SQL Server中创建了一个表,但是,如果我使用EF,该如何修复DbContext ,这不仅会在本地创建它,而且不是我想要的

Entity Framework, along with Migrations, will help you here. 实体框架以及迁移将在这里为您提供帮助。

I suggest you check out this tutorial: Code First to a New Database 我建议您检查一下本教程: 代码优先到新数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM