views:

23

answers:

1

Hi!

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; }
+1  A: 

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 set Mutable=false in Typ's [ActiveRecord]

Mauricio Scheffer
From this user point of view Typ is readonly so I go with ModelsCreatedEvent in initializer and there set Mutable = false. Apparently this solves the problem of unwanted updates :) Thanks :)
Adrian Serafin