本文介绍了如何使用现有的存储采购MVC插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在visual studio 2015 mvc 5 entityframework 6代码首先从现有数据库工作 我需要根据现有存储过程插入数据但我不知道如何这样做 下面的代码有什么问题 step 1 USE [EmployeeSystem] GO / * *****对象:StoredProcedure [dbo] 。[spAddDepartment]脚本日期:13/02/2018 12:32:38 AM ****** / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER 过程 [dbo]。[spAddDepartment] @ DepartmentName nvarchar ( 50 ) as 开始 将 插入部门 values ( @ DepartmentName , 1 ) 结束 step2 受保护 覆盖 void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity< Department>()。MapToStoredProcedures (s = > s.Insert(i = > i.HasName( spAddDepartment))); base .OnModelCreating(modelBuilder); } step3 public ActionResult Insert(Department depart) { // return View(); hr.Departments.Add(depart); hr.SaveChanges(); return 查看(离开); } 我的尝试: 如何使用现有存储的采购mvc插入数据 解决方案 有SqlQuery方法可用在实体框架中调用存储过程或执行sql语句。 请查看以下内容: https://stackoverflow.com/q/20901419 I work in visual studio 2015 mvc 5 entityframework 6 code first from existing database I need to insert data based on existing stored procedure but i dont know How to do thatwhat is wrong in code belowstep 1USE [EmployeeSystem]GO/****** Object: StoredProcedure [dbo].[spAddDepartment] Script Date: 13/02/2018 12:32:38 AM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER Procedure [dbo].[spAddDepartment]@DepartmentName nvarchar(50)as Begin Insert into Departments values(@DepartmentName,1) Endstep2protected override void OnModelCreating(DbModelBuilder modelBuilder){modelBuilder.Entity<Department>().MapToStoredProcedures(s => s.Insert(i => i.HasName("spAddDepartment")));base.OnModelCreating(modelBuilder);}step3public ActionResult Insert(Department depart){// return View();hr.Departments.Add(depart);hr.SaveChanges();return View(depart);}What I have tried:How to insert data using existing stored procure mvc 解决方案 There is SqlQuery method available in entity Framework to call the stored procedures or execute sql statements.Please have a look at following:https://stackoverflow.com/q/20901419 这篇关于如何使用现有的存储采购MVC插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 01:42