Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does asp.net use WebAPI and EF framework to realize the basic operation of data?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the "asp.net how to use WebAPI and EF framework to achieve the basic operation of data" related knowledge, Xiaobian through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "asp.net how to use WebAPI and EF framework to achieve the basic operation of data" article can help you solve the problem.

I. data preparation

Database script:

Create table Dept-Department information (DeptId int primary key identity (1Power1), DeptName varchar (50) not null) create table Employee-employee information (EmpId int primary key identity (1d1), DeptId int not null, EmpName varchar (50) not null, EmpPhone varchar (50) not null, EmpArea varchar (50) not null EmpSalary decimal (18Power2) not null) insert into Dept (DeptName) values ('Development Department') insert into Dept (DeptName) values ('Test Department') insert into Dept (DeptName) values ('implementation Department') insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (1pm 'Liu Dehua', '13887855552' 'Wuhan', 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (2Jing 'Zhang Xueyou', '13556528634', 'Shenzhen', 6500) insert into Employee (DeptId,EmpName) EmpPhone,EmpArea,EmpSalary) values (3jung 'Liu Yifei', '13448494546' Guangzhou', 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (1 'Jay Chou', '13888666855' 'Beijing', 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (2'Xu Wei', '138684219' Shanghai', 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (3min 'Stefanie Sun', '13895133572' Chengdu' 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (1pm 'Pushu', '13458788896'' Wuhan', 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (2meme 'Chow Yun Fat', '13554588745' 'Nanjing', 6500) insert into Employee (DeptId,EmpName,EmpPhone,EmpArea,EmpSalary) values (3pm 'Lianjie', '13998759654' Shanghai', 6500) select * from Dept Select * from Employee; II, data list

API interface:

[HttpGet] public IHttpActionResult Get () {var data = from emp in db.Employee join dept in db.Dept on emp.DeptId equals dept.DeptId select new {EmpId = emp.EmpId, DeptId = emp.DeptId, EmpName = emp.EmpName, EmpPhone = emp.EmpPhone EmpArea = emp.EmpArea, EmpSalary = emp.EmpSalary, DeptName = dept.DeptName} Return Json (new {res = 1, msg = "", data = data});}

Interface test address:

Http://localhost:1894/api/Employee III. Combinatorial conditional search

Entities that accept search criteria:

Public class SearchDto {public int DeptId {get; set;} / / Department number public string EmpName {get; set;} / / Department name}

API interface:

[HttpGet] [Route ("api/Employee/Search")] public IHttpActionResult Get ([FromUri] SearchDto searchDto) {var data = from emp in db.Employee join dept in db.Dept on emp.DeptId equals dept.DeptId select new {EmpId = emp.EmpId, DeptId = emp.DeptId, EmpName = emp.EmpName EmpPhone = emp.EmpPhone, EmpArea = emp.EmpArea, EmpSalary = emp.EmpSalary, DeptName = dept.DeptName} If (searchDto.DeptId > 0) data = data.Where (p = > p.DeptId = = searchDto.DeptId); if (! string.IsNullOrEmpty (searchDto.EmpName)) data = data.Where (p = > p.EmpName.Contains (searchDto.EmpName)); return Json (new {res = 1, msg = "", data = data});}

Interface testing:

Fourth, combination conditional search plus paging

Entities that accept search criteria:

Public class SearchDto {public int DeptId {get; set;} / / Department number public string EmpName {get; set;} / / Department name}

API interface:

[HttpGet] [Route ("api/Employee/SearchPage")] public IHttpActionResult Get (int page,int pagesize, [FromUri] SearchDto searchDto) {var query = from emp in db.Employee join dept in db.Dept on emp.DeptId equals dept.DeptId select new {EmpId = emp.EmpId, DeptId = emp.DeptId, EmpName = emp.EmpName EmpPhone = emp.EmpPhone, EmpArea = emp.EmpArea, EmpSalary = emp.EmpSalary, DeptName = dept.DeptName} If (searchDto.DeptId > 0) query = query.Where (p = > p.DeptId = = searchDto.DeptId); if (! string.IsNullOrEmpty (searchDto.EmpName)) query = query.Where (p = > p.EmpName.Contains (searchDto.EmpName)); var data = query.OrderByDescending (p = > p.EmpId) .ToPagedList (page, pagesize); return Json (new {res = 1, msg = ", total=data.TotalItemCount, data = data});}

Interface testing:

5. Addition of data

API interface solution 1: (directly use entities in EF to accept new data)

[HttpPost] / / to reduce the coupling between the front end and EF framework entities, you can also redefine the Dto class, and then map the Dto data to public IHttpActionResult Add (Employee emp) {db.Employee.Add (emp) in the Employee object in EF; int r = db.SaveChanges (); if (r = = 1) return Json (new {res = 1, msg = "added successfully"}) Else return Json (new {res = 1, msg = "add failed"});}

API interface scheme 2: (use Dto to accept data and directly realize the conversion from Dto object to EF entity through attribute assignment)

Public class EmpAddDto {public int EmpId {get; set;} public int DeptId {get; set;} public string EmpName {get; set;} public string EmpPhone {get; set;} public string EmpArea {get; set;} public decimal EmpSalary {get; set;}} [HttpPost] public IHttpActionResult Add (EmpAddDto dto) {/ / attributes are assigned sequentially to realize the conversion from Dto objects to EF entity objects Employee emp = new Employee (); emp.DeptId = dto.DeptId Emp.EmpName = dto.EmpName; emp.EmpPhone = dto.EmpPhone; emp.EmpArea = dto.EmpArea; emp.EmpSalary = dto.EmpSalary; db.Employee.Add (emp); int r = db.SaveChanges (); if (r = = 1) return Json (new {res = 1, msg = "add successful"}); else return Json (new {res = 1, msg = "add failed"});}

API interface scheme 3: (mapping through AutoMapper components)

AutoMapper components can be installed in NuGet, tested not to be used in framework4.5, and can be used normally in framework4.7.1.

If you do not have framework4.7.1 installed, you can download the .NET Framework 4.7.1 Developer Pack from the official website.

If you change the target frame to framework4.7.1 through the property panel of the project, the program may compile errors and need to modify the configuration file:

CompilerOptions= "/ langversion:6 / nowarn:1659;1699;1701"

Modified to:

CompilerOptions= "/ langversion:Default / nowarn:1659;1699;1701"

API interface code: (when the attribute names of the two types are exactly the same)

Public class EmpAddDto {public int EmpId {get; set;} public int DeptId {get; set;} public string EmpName {get; set;} public string EmpPhone {get; set;} public string EmpArea {get; set;} public decimal EmpSalary {get; set;}} [HttpPost] public IHttpActionResult Add (EmpAddDto dto) {/ / two types of attribute names are the same automatic mapping var config = new MapperConfiguration (cfg = > cfg.CreateMap ()) Var mapper = config.CreateMapper (); Employee emp = mapper.Map (dto); db.Employee.Add (emp); int r = db.SaveChanges (); if (r = = 1) return Json (new {res = 1, msg = "add success"}); else return Json (new {res = 1, msg = "add failure"});}

API API code: (when the attribute names of the two types are not the same)

Public class EmpAddDto {public int EmpId {get; set;} public int DeptId {get; set;} public string Name {get; set;} public string Phone {get; set;} public string EmpArea {get; set;} public decimal EmpSalary {get; set }} [HttpPost] public IHttpActionResult Add (EmpAddDto dto) {/ / both types have the same attribute automatic mapping var config = new MapperConfiguration (cfg = > {cfg.CreateMap () .ForMember ("EmpName", opt = > opt.MapFrom (src = > src.Name)) .ForMember (dest = > dest.EmpPhone, opt = > opt.MapFrom (src = > src.Phone)) / / .ForMember (dest = > dest.EmpId, opt = > opt.MapFrom (src = > src.EmpId)) / / .ForMember (dest = > dest.DeptId, opt = > opt.MapFrom (src = > src.DeptId)) / / .ForMember (dest = > dest.EmpName, opt = > opt.MapFrom (src = > src.Name) / / .ForMember (dest = > dest.EmpPhone, opt = > opt.MapFrom (src = > src.Phone)) / / .ForMember (dest = > dest.EmpArea) Opt = > opt.MapFrom (src = > src.EmpArea) / / .ForMember (dest = > dest.EmpSalary, opt = > opt.MapFrom (src = > src.EmpSalary)) }); var mapper = config.CreateMapper (); Employee emp = mapper.Map (dto); db.Employee.Add (emp); int r = db.SaveChanges (); if (r = = 1) return Json (new {res = 1, msg = "add success"}); else return Json (new {res = 1, msg = "add failed"});}

API test: (when using EmpAddDto to accept data, note that the parameter name should be the same as the attribute name of EmpAddDto)

This is the end of the introduction to "how asp.net uses WebAPI and EF framework to achieve the basic operation of data". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report