ASP.NET DynamicData:更新期间发生了什么?
我在我的网站上使用 ASP.NET DynamicData(基于 LINQ to SQL)作为基本脚手架。在一张表上,我添加了其他属性,这些属性不存储在表中,而是从其他地方检索。 (在本例中为用户帐户的配置文件信息)。
它们显示得很好,但是当编辑这些值并按“更新”时,它们不会改变。
属性如下所示,该表是标准的 aspnet_Users 表:
public String Address
{
get
{
UserProfile profile = UserProfile.GetUserProfile(UserName);
return profile.Address;
}
set
{
UserProfile profile = UserProfile.GetUserProfile(UserName);
profile.Address = value;
profile.Save();
}
}
当我启动调试器时,我注意到对于每次更新,set
访问器都会被调用三次。一次使用新值,但在新创建的用户实例上,然后一次使用旧值,再次在新实例上,最后在现有实例上使用旧值。
有点想知道,我检查了设计师创建的属性,它们也以(几乎)相同的方式被调用了三次。唯一的区别是,最后一次调用包含属性的新值。
我在这里有点困惑。为什么是三次,为什么我的新属性表现不同?对于此事的任何帮助,我将不胜感激! =)
I am using ASP.NET DynamicData (based on LINQ to SQL) on my site for basic scaffolding. On one table I have added additional properties, that are not stored in the table, but are retrieved from somewhere else. (Profile information for a user account, in this case).
They are displayed just fine, but when editing these values and pressing "Update", they are not changed.
Here's what the properties look like, the table is the standard aspnet_Users table:
public String Address
{
get
{
UserProfile profile = UserProfile.GetUserProfile(UserName);
return profile.Address;
}
set
{
UserProfile profile = UserProfile.GetUserProfile(UserName);
profile.Address = value;
profile.Save();
}
}
When I fired up the debugger, I've noticed that for each update the set
accessor is called three times. Once with the new value, but on a newly created instance of user, then once with the old value, again on an new instance, and finally with the old value on the existing instance.
Wondering a bit, I checked with the properties created by the designer, and they, too, are called three times in (almost) the same fashion. The only difference is, that the last call contains the new value for the property.
I am a bit stumped here. Why three times, and why are my new properties behaving differently? I'd be grateful for any help on that matter! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我让 Linq to SQL 使用存储过程进行插入/更新时,我观察到类似的情况。我不确定我是否记错了,但我认为 Linq to SQL 使用实体类的这三个实例来找出发生了什么变化,以便可以构建所需的 SQL 语句。
我基本上看到两个选项(尽管我不确定这是否真的有效):
该属性将如下所示:
在这两种情况下,您都会遇到问题,尽管实体其余部分的更新失败,但您可能会更新额外的字段。这当然也是您最初方法的问题。
我认为最好的解决方案是实现您自己的配置文件提供程序并将配置文件信息存储在您自己的表中。如果您这样做,您可以让 Linq to SQL 为您的个人资料信息创建实体:一切都将是“标准”,您不必诉诸某种“黑客”...
I observed something similar when I let Linq to SQL use stored procedures for inserting/updating. I am not sure if I remember correctly, but I think that Linq to SQL uses these three instances of the entity class to figure out what changed so that the required SQL statement can be built.
I see basically two options (though I am not sure if this really works):
The property would look then like this:
In both cases you have the problem that you might update the extra fields though the update of the rest of the entity fails. This was of course also a problem with your initial approach.
I think the best solution would be to implement your own profile provider and store the profile information in your own tables. If you do that you could let Linq to SQL create entities for your profile information: Everything would be "standard" and you would not have to resort to some kind of "hack"...