views:

135

answers:

1

I have a little problem with fluentNhibernate and MySQL. I would like to map my entity:

public class Topic
{
    public Topic()
    {
        ParentTopic = null;
    }

    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime CreatedAt { get; private set; }
    public virtual Guid CreatedBy { get; set; }
    public virtual IList<Post> Posts { get; set; }
    public virtual Topic ParentTopic { get; set; }
}

To a table with the same name. My bigest problem is how to I do the mapping of the CreatedAt so that in the database I get a timestamp that is only changed on insert and ignored when updating.

thx ;)

A: 

Found the answer to my little problem :)

public class TopicMap : ClassMap<Topic>
{
    public TopicMap()
    {
        Table("Topics");
        Id(t => t.Id).GeneratedBy.Guid();
        Map(t => t.CreatedAt).Not.Nullable().Generated.Insert().CustomSqlType("timestamp");
        Map(t => t.CreatedBy).Not.Nullable();
        Map(t => t.Name).Not.Nullable().Length(500);
        HasMany(t => t.Posts);
        References(t => t.ParentTopic).Nullable().Cascade.All().ForeignKey("FK_Topic_ParentTopic");
    }
}

This seams to work in my unit tests. Hope that it will not produce any greater problems in the future.

If anybody seas a problem with this then please let me know.

Dejan
Not a problem, just a suggestion. `GeneratedBy.Guid()` is unnecessary because Fluent NHibernate can figure out from the property how to generate it.
James Gregory