我想弄清楚如何在使用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參數。
希望這能更好地說明我正在嘗試做什麼。
我試圖與EF的ChangeTracker工作......我的想法是,我會重寫EF的Entity.Delete,因爲這就是我想EF會調用存儲過程,但我似乎無法弄清楚如何覆蓋它... – John 2014-10-16 16:42:58