是否有可能插入/更新前执行POCO行动? [英] Is it possible perform actions in POCO before insert/update?
问题描述
让我们假设这个实体框架示例:
Let's assume this Entity Framework sample:
public class User
{
public int UserID { get; set; }
public string Name { get; set; }
}
public class MyDbContext : DbContext
{
public DbSet<User> Users { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var db = new MyDbContext ())
{
var user = new User { Name = "Foo"};
db.Users.Add(user);
db.SaveChanges();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
我要添加到<强>用户类的事件,如BeforeInsert或更新前,而当代码...
I want add to User class events, like "BeforeInsert" or "BeforeUpdate", and when the code...
db.Users.Add(user);
...是任何地方的应用程序执行时,BeforeInsert的方法将得到提升。是否可以?
... is executed anywhere in the application, the "BeforeInsert" method will be raised. Is it possible?
推荐答案
@ JC的答案是正确的。我将只显示代码,在其中你可以做你想做的。首先,覆盖SaveChanges方法在上下文,并调用这个被覆盖的SaveChanges方法: context.SaveChanges(真)代替的 conntext.SaveChanges()的
@JC's answer is correct. I will just show code, in which you can do what you want. First of all, override SaveChanges method in your context, and call this overriden SaveChanges method: context.SaveChanges(true) instead of conntext.SaveChanges()
public class MyDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public int SaveChanges(bool performCustomOperations)
{
if(performCustomOperations)
{
CustomContextManager contextManager = new CustomContextManager(this);
//Perform operations you want before saving data
contextManager.PerformBeforeUpdate();
//To do: Add your own PerformBeforeInsert method to CustomContextManager class
}
//Save changes to underlying database
this.SaveChanges();
}
}
您CustomContextManager类应该是这样的。在 PerformBeforeUpdate 方式执行您之前更新操作。您可以添加自己的 PerformBeforeInsert和/或PerformBeforeDelete 的方法。
Your CustomContextManager class should look like this. Performing your before update operations in PerformBeforeUpdate method. You can add your own PerformBeforeInsert and/or PerformBeforeDelete methods.
public class CustomContextManager
{
private MyDbContext context;
public CustomContextManager(MyDbContext contextParam)
{
if (contextParam == null)
throw new ArgumentNUllException(nameof(contextParam));
this.context = contextParam;
}
public void PerformBeforeUpdate
{
//Get object context to be able to perform operations
System.Data.Objects.ObjectContext myObjectContext =
((IObjectContextAdapter)context).ObjectContext;
IEnumerable<ObjectStateEntry> updatedRecords =
taxesObjectContext.ObjectStateManager.GetObjectStateEntries(
System.Data.EntityState.Modified);
if (updatedRecords != null
&& updatedRecords.Count() > 0)
{
foreach (ObjectStateEntry stateEntry in updatedRecords)
{
if (stateEntry != null
&& stateEntry.Entity != null)
{
//Do what operations you want instead of below linnes
User modifiedUser = stateEntry.Entity as User;
if(modifiedUser != null)
modifiedUser.Name = "Altered before update";
}
}
}
}
}
这篇关于是否有可能插入/更新前执行POCO行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!