views:

62

answers:

3

Hi,

I have a scenario where I have a parent class with some definitions we can call if Foo to be unique and then a child class that I chose to call Bar. Together they make Foo Bar! :)

public class Foo
{
    public Foo()
    {
     Bars = new List<Bar>();
    }

    // Internal id only, should probably not even map it
    public virtual int Id { get; set; } 
    // A hashed searchable id
    public virtual string UId { get; set; } 
    public virtual DateTime CreatedAt { get; set; }
    public virtual DateTime UpdatedAt { get; set; }

    public ICollection<Bar> Bars { get; private set; }
}

public class Bar
{
    public virtual int Id { get; set; }
    public virtual long Version { get; set; }
    public virtual DateTime CreatedAt { get; set; }
    public virtual SomeHash { get; set; }
    public virtual Foo Foo { get; set; }
}

I can map this quite easily in both fluently and with xml. What I don't know how to map is the following scenario: I need to never update anything here. As soon as something changes I should just insert a new row. Never update or delete and I can't find any information about solving this even though I know this should indeed be possible. How do I tell NHibernate that every change in the version table should cause an insert?

+1  A: 

In Fluent you can call the Readonly property which maps to the access="readonly" attribute.

public class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
     ReadOnly();

     Id(x => x.Id).ReadOnly();
     // etc.
    }
}

I've never used it though, so I'm not sure it will work for what you need.

Chris S
It is what Josh refers to as Mutable. I prefer they had not changed all the names in fluent :)
mhenrixon
A: 

You could use blank <sql-update> and <sql-delete> tags in you mapping. these should get called when the class tries to update or delete. but the nhibernate should still do the insert

Nathan Fisher
+2  A: 

Take a look at the mutable property of the class element. According to the documentation, setting it to false prevents UPDATE and DELETE

Josh Pearce
And right you where!
mhenrixon