sql:
IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].DepartmentMaster') and OBJECTPROPERTY(id, N'IsUserTable') = 1) DROP TABLE DepartmentMaster GO create table DepartmentMaster ( DepartmentId INT IDENTITY(1,1) PRIMARY KEY, Code nvarchar(50), [Name] nvarchar(50) ) go insert into DepartmentMaster(Code,[Name]) values('0001','IT') go select * from DepartmentMaster IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].EmployeeMasters') and OBJECTPROPERTY(id, N'IsUserTable') = 1) DROP TABLE EmployeeMasters GO create table EmployeeMasters ( EmployeeId INT IDENTITY(1,1) PRIMARY KEY, Code nvarchar(50), [Name] nvarchar(50), [DepartmentId] int FOREIGN KEY REFERENCES DepartmentMaster(DepartmentId) ) go select * from EmployeeMasters go CREATE PROCEDURE [dbo].[InsertEmployee] @Code [nvarchar](max), @Name [nvarchar](max), @DepartmentId [int] AS BEGIN INSERT [dbo].[EmployeeMasters]([Code], [Name], [DepartmentId]) VALUES (@Code, @Name, @DepartmentId) DECLARE @EmployeeId int SELECT @EmployeeId = [EmployeeId] FROM [dbo].[EmployeeMasters] WHERE @@ROWCOUNT > 0 AND [EmployeeId] = scope_identity() SELECT t0.[EmployeeId] FROM [dbo].[EmployeeMasters] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[EmployeeId] = @EmployeeId END GO CREATE PROCEDURE [dbo].[EmployeeMaster_Insert] @Code [nvarchar](max), @Name [nvarchar](max), @DepartmentId [int] AS BEGIN INSERT [dbo].[EmployeeMasters]([Code], [Name], [DepartmentId]) VALUES (@Code, @Name, @DepartmentId) DECLARE @EmployeeId int SELECT @EmployeeId = [EmployeeId] FROM [dbo].[EmployeeMasters] WHERE @@ROWCOUNT > 0 AND [EmployeeId] = scope_identity() SELECT t0.[EmployeeId] FROM [dbo].[EmployeeMasters] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[EmployeeId] = @EmployeeId END GO CREATE PROCEDURE [dbo].[UpdateEmployee] @EmployeeId [int], @Code [nvarchar](max), @Name [nvarchar](max), @DepartmentId [int] AS BEGIN UPDATE [dbo].[EmployeeMasters] SET [Code] = @Code, [Name] = @Name, [DepartmentId] = @DepartmentId WHERE ([EmployeeId] = @EmployeeId) END GO CREATE PROCEDURE [dbo].[DeleteEmployee] @EmployeeId [int] AS BEGIN DELETE [dbo].[EmployeeMasters] WHERE ([EmployeeId] = @EmployeeId) END
/// <summary> /// geovindu,Geovin Du /// </summary> public class EmployeeMaster { /// <summary> /// /// </summary> [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int EmployeeId { get; set; } /// <summary> /// /// </summary> [StringLength(50)] public string Code { get; set; } /// <summary> /// /// </summary> [StringLength(50)] public string Name { get; set; } /// <summary> /// /// </summary> public int DepartmentId { get; set; } /// <summary> /// /// </summary> public virtual DepartmentMaster Department { get; set; } } using EntityFramework6.Entity; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.ModelConfiguration; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EntityFramework6.Map { /// <summary> /// https://learn.microsoft.com/zh-cn/ef/ef6/modeling/code-first/fluent/cud-stored-procedures /// https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/cud-stored-procedures /// ef6 存储过程映射 Entity Framework 6 /// Stored procedure mapping /// Code First Insert, Update, and Delete Stored Procedures /// </summary> public class EmployeeMasterMap: EntityTypeConfiguration<EmployeeMaster> { /// <summary> /// /// </summary> public EmployeeMasterMap() { //table ToTable("EmployeeMaster"); //key HasKey(t => t.EmployeeId); //fields Property(x => x.EmployeeId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); Property(x => x.Name).HasColumnType("VARCHAR").HasMaxLength(50); Property(x => x.Code).HasColumnType("VARCHAR").HasMaxLength(50); Property(x => x.DepartmentId) .HasColumnName("DepartmentId") .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); HasRequired(c => c.Department).WithMany(l => l.Employees); //Stored procedure mapping 存储过程 i => i.HasName("InsertEmployee") // .Parameter(b => b.Code, "Code") // .Parameter(b => b.Name, "Name") // .Parameter(b => b.DepartmentId, "DepartmentId") /*1 规则默认存储过程 表名,类名,参数名一致 CREATE PROCEDURE [dbo].[EmployeeMaster_Insert] @Code [nvarchar](max), @Name [nvarchar](max), @DepartmentId [int] AS BEGIN INSERT [dbo].[EmployeeMasters]([Code], [Name], [DepartmentId]) VALUES (@Code, @Name, @DepartmentId) DECLARE @EmployeeId int SELECT @EmployeeId = [EmployeeId] FROM [dbo].[EmployeeMasters] WHERE @@ROWCOUNT > 0 AND [EmployeeId] = scope_identity() SELECT t0.[EmployeeId] FROM [dbo].[EmployeeMasters] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[EmployeeId] = @EmployeeId END GO //规则默认存储过程 表名,类名,参数名一致 //dbo.EmployeeMaster_Insert //MapToStoredProcedures(); */ //2 自定义存储过程 MapToStoredProcedures(s=>s.Insert(u=>u.HasName("InsertEmployee") //添加 .Parameter(b => b.Code, "Code") .Parameter(b => b.Name, "Name") .Parameter(b => b.DepartmentId, "DepartmentId") ) .Delete(d => d.HasName("DeleteEmployee") //删除 .Parameter(b => b.EmployeeId, "EmployeeId")) .Update(u => u.HasName("UpdateEmployee") //更新 .Parameter(b => b.EmployeeId, "EmployeeId") .Parameter(b => b.Code, "Code") .Parameter(b => b.Name, "Name") .Parameter(b => b.DepartmentId, "DepartmentId")) ); /*2 自定义存储过程 CREATE PROCEDURE [dbo].[InsertEmployee] @Code [nvarchar](max), @Name [nvarchar](max), @DepartmentId [int] AS BEGIN INSERT [dbo].[EmployeeMasters]([Code], [Name], [DepartmentId]) VALUES (@Code, @Name, @DepartmentId) DECLARE @EmployeeId int SELECT @EmployeeId = [EmployeeId] FROM [dbo].[EmployeeMasters] WHERE @@ROWCOUNT > 0 AND [EmployeeId] = scope_identity() SELECT t0.[EmployeeId] FROM [dbo].[EmployeeMasters] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[EmployeeId] = @EmployeeId END GO */ } } }
调用:
using (var ctx = new EfDbContext()) { ctx.Database.Log = Console.WriteLine; InitialEntities initialEntities = new InitialEntities(); initialEntities.Up(); //未找到存储过程 SqlParameter [] parameters = { new SqlParameter("@Code",SqlDbType.NVarChar, 50), new SqlParameter("@Name",SqlDbType.NVarChar,50), new SqlParameter("@DepartmentId",SqlDbType.Int) }; EmployeeMaster employee = new EmployeeMaster(); employee.Code = "A0011"; employee.Name = "Geovin Du 涂聚文"; employee.DepartmentId = 1; parameters[0].Value= employee.Code; parameters[1].Value= employee.Name; parameters[2].Value = employee.DepartmentId; //不映射存储过程方式 //int k=ctx.Database.ExecuteSqlCommand("dbo.InsertEmployee @Code,@Name,@DepartmentId", parameters); // if(k>0) // { // Console.WriteLine("ok"); // } // else // { // Console.WriteLine("no"); // } //dbo.EmployeeMaster_Insert int k = 0; //存储过程 添加 ctx.Employees.Add(employee); k = ctx.SaveChanges(); if (k > 0) { Console.WriteLine("成功!"); } else { Console.WriteLine("不成功"); } //修改 SqlParameter[] eparameters = { new SqlParameter("@EmployeeId",SqlDbType.Int), new SqlParameter("@Code",SqlDbType.NVarChar, 50), new SqlParameter("@Name",SqlDbType.NVarChar,50), new SqlParameter("@DepartmentId",SqlDbType.Int) }; EmployeeMaster editemployee = new EmployeeMaster(); editemployee.EmployeeId = 2; editemployee.Code = "A0022"; editemployee.Name = "涂聚文"; editemployee.DepartmentId = 1; eparameters[0].Value = editemployee.EmployeeId; eparameters[1].Value = editemployee.Code; eparameters[2].Value = editemployee.Name; eparameters[3].Value = editemployee.DepartmentId; ctx.Entry(editemployee).State = System.Data.Entity.EntityState.Modified; k = ctx.SaveChanges(); if (k > 0) Console.WriteLine("ok,eidt成功"); else Console.WriteLine("no,eidt不成功"); EmployeeMaster delemployee = new EmployeeMaster(); delemployee.EmployeeId = 3; //删除 SqlParameter[] delparameters = { new SqlParameter("@EmployeeId",SqlDbType.Int), }; delparameters[0].Value=delemployee.EmployeeId.ToString(); ctx.Entry(delemployee).State = System.Data.Entity.EntityState.Deleted; k = ctx.SaveChanges(); if (k > 0) Console.WriteLine("ok del"); else Console.WriteLine("no del");
标签:donet,Code,EmployeeId,dbo,mapping,Entity,DepartmentId,EmployeeMasters,Name From: https://www.cnblogs.com/geovindu/p/17096092.html