开发者

Mapping a DateTime property to a timestamp mysq+fluentnhibernate

开发者 https://www.devze.com 2022-12-19 21:22 出处:网络
I have a little problem with fluentNhibernate and MySQL. I would like to map my entity: public class Topic

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. M开发者_JS百科y 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 ;)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号