使用asp.net 开发webapi 连通sql server

本文档详细介绍了如何使用ASP.NET Web API创建员工服务,包括项目创建、数据实体模型设置、引用类库、控制器添加及Entity Framework配置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建项目。参考2019-12-03日的博客。

2、右击解决方案新建项目。

3、右击删除“Class1.cs”。

4、新建数据实体模型。

 

 

5、引用EmployeeDataAccess的类库。

重新生成employdataaccess

6、添加controller的控制器。

 

控制器名称为:EmployeesController。

namespace EmployeeService.Controllers
{
    public class EmployeesController : ApiController
    {
        public IEnumerable<Employee> Get()
        {
            using (EmployeeDBEntities entities = new EmployeeDBEntities())
            {
                return entities.Employees.ToList();
            }
        }
    }
}

使得entityFramewok的版本一致。

在web.config中加入employeeentities的链接字串。(从app.config中拷贝过来。)

<connectionStrings>
    <add name="EmployeeDBEntities" connectionString="metadata=res://*/EmployeeDataModel.csdl|res://*/EmployeeDataModel.ssdl|res://*/EmployeeDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localDB)\MSSQLLocalDB;initial catalog=EmployeeDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

结果。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值