HI, Wit linq我在DAL中填充通用列表。然后在UI层中,用户从列表中删除并更新项目。是否有可能让LINQ跟踪更改,而不必编写代码来检查哪些项目已被删除并删除它们/添加新项目并更新其他项目等等。看起来,这在使用datacontext时不可能实现独立的datacontext是负责SubmitChanges。任何帮助赞赏。LINQ跟踪更改
感谢 尼尔
HI, Wit linq我在DAL中填充通用列表。然后在UI层中,用户从列表中删除并更新项目。是否有可能让LINQ跟踪更改,而不必编写代码来检查哪些项目已被删除并删除它们/添加新项目并更新其他项目等等。看起来,这在使用datacontext时不可能实现独立的datacontext是负责SubmitChanges。任何帮助赞赏。LINQ跟踪更改
感谢 尼尔
如果您使用的是Linq.DataContext
,那么它确实会跟踪您的变化。
要引用的函数是System.Data.Linq.DataContext.GetChangeSet。尽管可以在任何时候调用GetChangeSet
,但最好在MSDN文档中引用Remarks部分。
您可以检查/采取提交操作...
这里是你如何能覆盖在DataContext
SubmitChanges
,并采取在该点的动作一个简单的例子。
的例子表明,你可以修改数据前的最后提交,您还可以跟踪并采取特别行动时,特定成员被更新:
namespace ExampleProject1.Models
{
public partial class ExampleProject1DataContext
{
public override void SubmitChanges(ConflictMode failureMode)
{
ManangeAndProcessChangeSet(base.GetChangeSet());
base.SubmitChanges(failureMode);
}
private void ManangeAndProcessChangeSet(ChangeSet changeSet)
{
DateTime now = DateTime.UtcNow;
if (changeSet.Inserts.Count > 0)
{
ProcessInserts(changeSet.Inserts, now);
}
if (changeSet.Updates.Count > 0)
{
ProcessUpdates(changeSet.Updates, now);
}
if (changeSet.Deletes.Count > 0)
{
ProcessDeletes(changeSet.Deletes, now);
}
}
private void ProcessInserts(IList<object> list, DateTime now)
{
IEnumerable<Cake> cakes = list.OfType<Cake>();
IEnumerable<Topping> toppings = list.OfType<Topping>();
// Update the created and modified times.
foreach (Cake cake in cakes)
{
cake.Guid = Guid.NewGuid();
cake.CreatedDateTime = now;
cake.ModifiedDateTime = now;
}
foreach (Topping topping in toppings)
{
topping.Guid = Guid.NewGuid();
topping.CreatedDateTime = now;
topping.ModifiedDateTime = now;
}
}
private void ProcessUpdates(IList<object> list, DateTime now)
{
IEnumerable<Cake> cakes = list.OfType<Cake>();
IEnumerable<Topping> toppings = list.OfType<Topping>();
// Update the created and modified times.
foreach (Cake cake in cakes)
{
ProcessUpdates(cake, now);
}
foreach (Topping topping in toppings)
{
topping.ModifiedDateTime = now;
}
}
private void ProcessDeletes(IList<object> list, DateTime now)
{
IEnumerable<Cake> cakes = list.OfType<Cake>();
IEnumerable<Topping> toppings = list.OfType<Topping>();
// Could create tasks here to delete associated stored files for cakes and toppings.
}
private bool ProcessUpdates(Cake cake, DateTime now)
{
bool modified = false;
ModifiedMemberInfo[] mmi = context.Cakes.GetModifiedMembers(cake);
foreach (ModifiedMemberInfo mi in mmi)
{
switch (mi.Member.Name)
{
case "CountOfItemsSold":
// Exclude from updating the modified date.
break;
case "IsExpired":
if ((bool)mi.CurrentValue)
{
cake.ExpiredDateTime = now;
}
else
{
cake.ExpiredDateTime = null;
}
modified = true;
break;
default:
modified = true;
break;
}
}
if (modified)
{
cake.ModifiedDateTime = now;
}
return modified;
}
}
}
目前还不清楚您所使用的编辑机制,但如果多个DataContext
都参与其中,是的:你必须告诉删除的提交数据上下文(通过DeleteOnSubmit
)。当然,它需要知道每更改 - 否则它不会知道更改数据库。
我使用多个DataContext的,所以我需要告诉datacontext的变化是不理想的。我宁愿如果LINQ照顾这个对我来说,但不幸它dosent.thanks你的帮助 – Somedeveloper 2010-11-20 02:25:24
如果你有一个N层应用程序,这是不可能的,因为你将以断开的方式进行更新,删除和插入。
换句话说,将使用一个数据上下文来创建列表,实体将通过WCF或其他机制传递给客户端,然后发送回DAL,另一个数据上下文将用于做更新。
为什么蛋糕和浇头?那么,为什么不呢? :-) – 2010-11-23 01:02:01