[英]C# ADO.NET Entity
Here is the question : I have following entities: 问题是:我有以下实体:
Every time I update or insert a transaction I would like to autodetect the category depending on ReferenceMappingExpression
or DescriptionMapppingExpression
in CategoryMappings
Entity. 每次更新或插入事务时,我都希望根据CategoryMappings
实体中的ReferenceMappingExpression
或DescriptionMapppingExpression
自动检测类别。
I mean, I want to match Transaction.Description
to CategoryMappings.DescriptionMapping
and if it matches get the FkCategoryID
from CategoryMapping
and save the transactions. 我的意思是,我想将Transaction.Description
匹配到CategoryMappings.DescriptionMapping
,如果匹配, FkCategoryID
CategoryMapping
获取FkCategoryID
并保存交易。
It is possible to loop through every transaction in list and categorymapping
list but I don't think its good idea. 可以遍历列表和categorymapping
列表中的每个事务,但我认为这不是个好主意。 How would you do this? 你会怎么做? Any suggestions? 有什么建议么? Any experience of that? 有什么经验吗?
You can use ObjectStateManager, add a partial OnContextCreated method to your entity context. 您可以使用ObjectStateManager,将部分OnContextCreated方法添加到您的实体上下文中。 Add new handler to SavingChanges event of context. 将新的处理程序添加到上下文的SavingChanges事件。 Get all added and modified transactions there and do whatever you want inside it. 在此获取所有添加和修改的事务,然后在其中执行任何操作。 Like this: 像这样:
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.