2014-10-16 77 views
0

我想弄清楚如何在使用MapToStoredProcedures时将参数注入实体框架6中。这甚至有可能吗?如何注入EF6/Code First?

我只是想将我当前登录的用户名从应用程序传递到存储过程,但我似乎无法弄清楚哪里的EF6做了实际的调用。

编辑:更多的信息

好,因此,没有MapToStoredProcedures(又名让EF6只是使用表直接),我可以做我的覆盖SaveChangesAsync方法如下:

For Each Entry As DbEntityEntry In Me.ChangeTracker.Entries().Where(Function(o) o.State = EntityState.Deleted) 
    If TypeOf Entry.Entity Is ISoftDelete Then 
     'Implements Soft Delete interface, so let's do what needs doing. 

     Select Case Entry.Entity.GetType() 
      Case GetType(OS) 
       Dim _thisOS As OS = TryCast(Entry.Entity, OS) 

       Using db As New AppRegistrationContext 
        _thisOS = Await db.OSSet.Include("OSType").FirstOrDefaultAsync(Function(o) o.ID = _thisOS.ID) 
       End Using 

         If Not _thisOS Is Nothing Then 
          Try 
           Entry.Reference("OSType").CurrentValue = _thisOS.OSType 
          Catch ex As Exception 
           Debug.Print(ex.ToString) 
          End Try 

         End If 
        Case GetType(Server) 

        Case Else 
         'Do nothing - only filling in extra information for those that we need to 
       End Select 

       'Set the archival bits 
       Entry.Property("Archive").CurrentValue = True 
       Entry.Property("ArchiveDate").CurrentValue = Date.Now 
       Entry.Property("ArchiveBy").CurrentValue = HttpContext.Current.User.Identity.Name.ToString() 

       'Mark it modified 
       Entry.State = EntityState.Modified 

      End If 
     Next 

Return Await MyBase.SaveChangesAsync() 

好吧,该工程伟大的代表EF的直接表操作。

我想要做的,而是在存储过程中处理所有这些 - 但我需要通过HttpContext.Current.User.Identity.Name.ToString()与我的删除存储过程来设置ArchiveBy参数。

希望这能更好地说明我正在尝试做什么。

回答

0

生活对你来说不是那么容易。运行类似以下内容:

在你的资料库添加以下内容:

public void ExecuteSqlCommand(string sql, params object[] parameters) 
    { 
     DbContext.Database.ExecuteSqlCommand(sql, parameters); 
    } 

,并使用它,就像下面:

public void DoSomething(int officeId) 
    { 
     var sqlParam = new SqlParameter("p0", officeId); 

     var parameters = new object[] { sqlParam }; 

     ((GenericRepository)Repository).ExecuteSqlCommand("EXEC dbo.myProc @p0", parameters); 
    } 

或者干脆就叫

DbContext.Database.ExecuteSqlCommand 

正如我根据您的需求在上面展示的那样。

更新1:你想一个存储过程来照顾CRUD业务:

假设你的环境中称之为:MyDbContext

然后声明有点像在部分MyDbContext类以下内容:

public partial class MyDbContext 
{ 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     modelBuilder 
     .Entity<SomeCustomEntity>() 
     .MapToStoredProcedures(agent => 
     { 
      agent.Insert(i => i.HasName("spr_MyInsert")); 
      agent.Update(u => u.HasName("spr_MyUpdate")); 
      agent.Delete(d => d.HasName("spr_MyDelete")); 
     }); 
    } 
} 

现在,每当你想要做一些CRUD程序,你的操作将通过存储过程[你映射的那个]运行,你不必担心ABO UT递东西给存储过程:

using (var context = new MyDbContext()) 
{ 
    context.SomeCustomEntity.Add(new SomeCustomEntity 
    { 
     Name = "Jack Something", 
     Phone = "999" 
    }); 

    context.SaveChanges(); 
} 
+0

我试图与EF的ChangeTracker工作......我的想法是,我会重写EF的Entity.Delete,因为这就是我想EF会调用存储过程,但我似乎无法弄清楚如何覆盖它... – John 2014-10-16 16:42:58