C#ADO.NET实体
问题是:我有以下实体: 每次更新或插入事务时,我都希望根据C#ADO.NET实体,c#,.net,entity-framework,C#,.net,Entity Framework,问题是:我有以下实体: 每次更新或插入事务时,我都希望根据CategoryMappings实体中的ReferenceMappingExpression或descriptionmappingexpression自动检测类别 我的意思是,我想将Transaction.Description与CategoryMapping.DescriptionMapping匹配,如果匹配,则从CategoryMapping中获取FkCategoryID,并保存事务 可以循环查看列表和categorymapping
CategoryMappings
实体中的ReferenceMappingExpression
或descriptionmappingexpression
自动检测类别
我的意思是,我想将Transaction.Description
与CategoryMapping.DescriptionMapping
匹配,如果匹配,则从CategoryMapping
中获取FkCategoryID
,并保存事务
可以循环查看列表和
categorymapping
list中的每个事务,但我认为这不是个好主意。你会怎么做?有什么建议吗?有没有这样的经验?您可以使用ObjectStateManager,将部分OnContextCreated方法添加到实体上下文中。将新处理程序添加到上下文的SavingChanges事件。获取所有添加和修改的事务,并在其中执行任何您想要的操作。像这样:
public partial class ModelContainer
{
partial void OnContextCreated()
{
this.SavingChanges += new EventHandler(ModelContainer_SavingChanges);
}
void ModelContainer_SavingChanges(object sender, EventArgs e)
{
foreach (var item in this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added))
{
SetTransactionDescription(item);
}
foreach (var item in this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified))
{
SetTransactionDescription(item);
}
}
void SetTransactionDescription(System.Data.Objects.ObjectStateEntry entry)
{
Transaction transaction = entry.Entity as Transaction;
if (transaction != null)
{
// Your code
}
}
}