CastleActiveRecord 中不必要的更新
我有两个 ActiveRecord 类,
class Product : ActiveRecordBase<Product>
{
private IList<Usage> _usages = new List<Usage>();
...
[HasMany(
ColumnKey="produkt_id",
Inverse=true,
Cascade=ManyRelationCascadeEnum.None)]
public IList<Usage> Usages
{
get { return _usages; }
set { _usages = value; }
}
...
}
class Usage : ActiveRecordBase<Usage>
{
private Product _product;
[BelongsTo("product_id")]
public Product Product
{
get { return _product; }
set { _product = value; }
}
private Typ _typ;
[BelongsTo("typ_id")]
public Typ
{
get { return _typ; }
set { _typ = value; }
}
}
public class Typ : ActiveRecordBase<Typ> {}
这代表可以在某些类型 (Typ) 的汽车中使用(使用)的汽车零件(产品)。
我有 gui,左侧有 datagridview 中的产品,右侧有 datagridview 的用法。在产品的 OnSelectionChanged 事件中,我这样做:
productUsagesDataGridView.DataSource = _currentProduct.Usages;
当我更改产品中的其他一些内容(而不是用法)并调用:
_currentProduct.SaveAndFlush();
活动记录对每个产品的使用行进行 sql 更新,更奇怪的是,在每个类型行上:/我100% 确定我没有更改任何这些行。出现问题是因为连接到数据库的用户没有表类型的更新权限,并且应该保持这种状态。
也许你们中的一些人知道我该如何防止这种情况发生?
[解决方案]
无论如何,这都是我的错。 Typ 在数据库和 ar 模型中具有created_at 和modified_at 列,
DateTime CreatedAt { get; set; }
DateTime ModifiedAt { get; set; }
因此在数据库中这些列有时可能为空,当我将它们加载到应用程序中时,它们被设置为DateTime.Min。
解决方案是将模型中的字段类型更改为可为空:
DateTime? CreatedAt { get; set; }
DateTime? ModifiedAt { get; set; }
I have two ActiveRecord classes
class Product : ActiveRecordBase<Product>
{
private IList<Usage> _usages = new List<Usage>();
...
[HasMany(
ColumnKey="produkt_id",
Inverse=true,
Cascade=ManyRelationCascadeEnum.None)]
public IList<Usage> Usages
{
get { return _usages; }
set { _usages = value; }
}
...
}
class Usage : ActiveRecordBase<Usage>
{
private Product _product;
[BelongsTo("product_id")]
public Product Product
{
get { return _product; }
set { _product = value; }
}
private Typ _typ;
[BelongsTo("typ_id")]
public Typ
{
get { return _typ; }
set { _typ = value; }
}
}
public class Typ : ActiveRecordBase<Typ> {}
This represents car parts (products) that can be used (usage) in certain types (Typ) of cars.
I have gui with products in datagridview on the left side and usages datagridview on the right side. In OnSelectionChanged event in products I do:
productUsagesDataGridView.DataSource = _currentProduct.Usages;
And when i changed some other things in product (not usages) and call:
_currentProduct.SaveAndFlush();
active record do sql updates on each product's usage row and, what's more wierd, on each typ row :/ And I'm 100% sure that I didn't change any of this rows. Problem came up becouse user that connects to db doesn't have update rights on table typ and it should stay that way.
Maybe some of you know how can I prevent this?
[Solution]
It occured that was my fault anyway. Typ has created_at and modified_at columns in database and in ar model
DateTime CreatedAt { get; set; }
DateTime ModifiedAt { get; set; }
so when in database those columns can sometimes be null, when I loaded them into application they were set to DateTime.Min.
The solution is to change field types in model to nullable:
DateTime? CreatedAt { get; set; }
DateTime? ModifiedAt { get; set; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有时,不需要的更新可能是由映射不匹配引起的(请参阅 这篇文章对一个这样的案例进行了讨论和解决)。
OTOH 如果
Typ
确实是只读的,您可能需要在Typ
的[ActiveRecord]
中设置Mutable=false
Unwanted updates can be caused sometimes by mapping mismatches (see this article for a discussion and solution of one such case).
OTOH if
Typ
is really readonly you might want to setMutable=false
inTyp
's[ActiveRecord]