CSharp: donet 7 create Stored procedure mapping with Entity Framework core 7
sql:
IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].People') and OBJECTPROPERTY(id, N'IsUserTable') = 1) DROP TABLE People GO CREATE TABLE People ( Id Int IDENTITY(1,1) Primary Key, [Name] nvarchar(50), Name_Original [nvarchar](100) ) go select * from People go ---存储过程映射 drop PROCEDURE [dbo].[People_Insert] go CREATE PROCEDURE [dbo].[People_Insert] @Name [nvarchar](max), @Name_Original [nvarchar](max) AS BEGIN INSERT INTO [People] ([Name],Name_Original) OUTPUT INSERTED.[Id] VALUES (@Name,@Name_Original); END go drop PROCEDURE [dbo].[People_Update] go CREATE PROCEDURE [dbo].[People_Update] @Id [int], @Name_Original [nvarchar](max), @Name [nvarchar](max) AS BEGIN UPDATE [People] SET [Name] = @Name,Name_Original = @Name_Original WHERE [Id] = @Id --AND [Name_Original] = @Name_Original SELECT @@ROWCOUNT END go drop PROCEDURE [dbo].[People_Delete] go CREATE PROCEDURE [dbo].[People_Delete] @Id [int] --@Name_Original [nvarchar](max) AS BEGIN DELETE FROM [People] OUTPUT 1 WHERE [Id] = @Id --AND [Name_Original] = @Name_Original; END
[Table("People")] public class People { public People() { } [Key] public int Id { get;set;} [StringLength(50)] public string Name { get;set;} [StringLength(100)] public string Name_Original { get;set;} }
// EF core 6.0 存储过程映射 //modelBuilder.Entity<Student>() // .MapToStoredProcedures(p => p.Insert(sp => sp.HasName("sp_InsertStudent").Parameter(pm => pm.StudentName, "name").Result(rs => rs.StudentId, "Id")) // .Update(sp => sp.HasName("sp_UpdateStudent").Parameter(pm => pm.StudentName, "name")) // .Delete(sp => sp.HasName("sp_DeleteStudent").Parameter(pm => pm.StudentId, "Id")) // ); // EF core 7.0 存储过程映射 //modelBuilder.Entity<DuProduct>() //.InsertUsingStoredProcedure( // "DuProduct_Insert", // storedProcedureBuilder => // { // storedProcedureBuilder.HasParameter(duproduct => duproduct.Name); // storedProcedureBuilder.HasResultColumn(duproduct => duproduct.Id); // storedProcedureBuilder.HasParameter(duproduct => duproduct.Price); // storedProcedureBuilder.HasParameter(duproduct => duproduct.Quantity); // }) //.UpdateUsingStoredProcedure( // "DuProduct_Update", // storedProcedureBuilder => // { // storedProcedureBuilder.HasOriginalValueParameter(duproduct => duproduct.Id); // storedProcedureBuilder.HasOriginalValueParameter(duproduct => duproduct.Name); // storedProcedureBuilder.HasParameter(duproduct => duproduct.Name); // storedProcedureBuilder.HasParameter(duproduct => duproduct.Price); // storedProcedureBuilder.HasParameter(duproduct => duproduct.Quantity); // storedProcedureBuilder.HasRowsAffectedResultColumn(); // }) //.DeleteUsingStoredProcedure( // "DuProduct_Delete", // storedProcedureBuilder => // { // storedProcedureBuilder.HasOriginalValueParameter(duproduct => duproduct.Id); // // storedProcedureBuilder.HasOriginalValueParameter(duproduct => duproduct.Name); // storedProcedureBuilder.HasRowsAffectedResultColumn(); // }); modelBuilder.Entity<People>() .InsertUsingStoredProcedure( //添加 "People_Insert", storedProcedureBuilder => { storedProcedureBuilder.HasParameter(a => a.Name); storedProcedureBuilder.HasParameter(a => a.Name_Original); storedProcedureBuilder.HasResultColumn(a => a.Id); }) .UpdateUsingStoredProcedure( //修改 "People_Update", storedProcedureBuilder => { storedProcedureBuilder.HasOriginalValueParameter(person => person.Id); //HasOriginalValueParameter() //storedProcedureBuilder.HasOriginalValueParameter(person => person.Name); // storedProcedureBuilder.HasParameter(person => person.Id); storedProcedureBuilder.HasParameter(person => person.Name); storedProcedureBuilder.HasParameter(person => person.Name_Original); //storedProcedureBuilder.HasRowsAffectedResultColumn(); }) .DeleteUsingStoredProcedure( "People_Delete", storedProcedureBuilder => { storedProcedureBuilder.HasOriginalValueParameter(person => person.Id); // storedProcedureBuilder.HasOriginalValueParameter(person => person.Name_Original); //storedProcedureBuilder.HasParameter(person => person.Name_Original); //storedProcedureBuilder.HasRowsAffectedResultColumn(); }) ; //返回值 // .UpdateUsingStoredProcedure( // "DuProduct_Update", //storedProcedureBuilder => //{ // storedProcedureBuilder.HasOriginalValueParameter(duproduct => duproduct.Id); // storedProcedureBuilder.HasOriginalValueParameter(duproduct => document.RowVersion); // storedProcedureBuilder.HasParameter(duproduct => duproduct.Title); // storedProcedureBuilder.HasParameter(duproduct => duproduct.RowVersion, parameterBuilder => parameterBuilder.IsOutput()); // storedProcedureBuilder.HasRowsAffectedResultColumn(); //}); base.OnModelCreating(modelBuilder); }
调用:
DuDbContext context = new DuDbContext(); People people = new People(); // people.Id= 1; people.Name = "涂聚文Geovin Du"; people.Name_Original = "china"; context.People.Add(people); int k=context.SaveChanges(); if(k>0) { Console.WriteLine("ok"); } else { Console.WriteLine("no"); } var dupeople = context.People.Where(b => b.Id == 1).First(); if(!object.Equals(dupeople, null)) { //dupeople=new People(); //dupeople.Id = 1; dupeople.Name = "Geovin Du,涂聚文"; dupeople.Name_Original = "chinasz"; context.Entry(dupeople).State = EntityState.Modified; k = context.SaveChanges(); if (k > 0) { Console.WriteLine("edit,ok"); } else { Console.WriteLine("edit no"); } } var delpeople = context.People.Where(b => b.Id == 12).First(); context.Entry(delpeople).State = EntityState.Deleted; k = context.SaveChanges(); if (k > 0) { Console.WriteLine("del,ok"); } else { Console.WriteLine("del no"); }
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)