版本作为时间戳在流利的NHibernate / SQL Server中 [英] Version as timestamp in Fluent NHibernate / SQL Server

查看:157
本文介绍了版本作为时间戳在流利的NHibernate / SQL Server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用FNH w / SQL Server 2008,我试图添加一个版本作为时间戳,但是运行到SQLDateTime溢出错误,因为该值被传递为1/1/0001 12:00:00 AM。我发现这个(也引用这里),但仍然遇到这个问题。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ {$ $ $ $ $ $ $ $ $ {
$ Int64 Id {get;组; }
public virtual DateTime Version {get;组; }
}

//实体基本映射
公共抽象类EntityBaseMap< T> :ClassMap< T>其中T:EntityBase
{
public EntityBaseMap()
{
Id(x => x.Id).GeneratedBy.Identity();
OptimisticLock.Version();
版本(x => x.Version)
.CustomType(Timestamp);


$ b code $
$ b $ SQL $数据类型是约会时间。




$ b

编辑:实际的保存代码的操作方法

preublic $ ActionResult Create )
{
int currMaxSortOrder = session.CreateCriteria(typeof(Section))
.SetProjection(Projections.ProjectionList().Add(Projections.Max(Sortorder)))
.UniqueResult< int>();
SectionViewModel sectionViewModel = new SectionViewModel();
sectionViewModel.Sortorder = currMaxSortOrder + 1;
return View(Create,_AdminLayout,sectionViewModel);

$ b $ H $ $ $ b $ public ActionResult Create(SectionViewModel sectionInputModel)
{
if(ModelState.IsValid)
{
section = new Section();
Mapper.Map(sectionInputModel,section);
using(var tx = session.BeginTransaction())
{
session.SaveOrUpdate(section);
tx.Commit();
}
return RedirectToAction(index,pages)。WithFlash(new {success =Section'+ section.Name +'was added added。));
}
return View(Create,_AdminLayout,section);
}

编辑2:新增实体&映射

  public class Section:EntityBase 
{
public virtual String Name {get;组; }
public virtual int Sortorder {get;组; }
public virtual String RedirectUrl {get;组; }
公共虚拟IList< Page> Pages {get;组; }
$ b $ public Section()
{
Pages = new List< Page>();
}

public virtual void AddPage(Page page)
{
page.Section = this;
this.Pages.Add(page);
}
}

public class SectionMap:EntityBaseMap< Section>
{
public SectionMap()
{
Map(x => x.Name);
Map(x => x.Sortorder);
Map(x => x.RedirectUrl);
//一对多关系
HasMany(x => x.Pages)
.Inverse()
.Cascade.All();




$ div class =h2_lin>解决方案

不好意思!时刻



(添加这个以防像我这样的其他n00bs遇到同样的问题)

我终于深入挖掘,并意识到我已经配置它使用AutoMapping,而我正在创建只能与FluentMapping一起使用的地图。恢复使用FluentMapping和版本开始工作完美!

我猜我可以使用AutoMapping并添加一个约定,将对待一个名为Version与CustomType (时间戳),但现在我要使用FluentMapping,直到我得到更多的速度。


Using FNH w/ SQL Server 2008, I'm trying to add a Version as a timestamp, but running into the SQLDateTime Overflow error because the value is passed as 1/1/0001 12:00:00 AM. I found this (also referenced here), but still experiencing the problem.

// entity base
public abstract class EntityBase
{
    public virtual Int64 Id { get; set; }
    public virtual DateTime Version { get; set; }
}

// entity base map
public abstract class EntityBaseMap<T> : ClassMap<T> where T : EntityBase
{
    public EntityBaseMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        OptimisticLock.Version();
        Version(x => x.Version)
           .CustomType("Timestamp");

    }
}    

The SQL Server data type is "datetime".

I'm guessing its something small and stupid, but haven't found the cause yet - what am I missing?

EDIT: Action method for the actual "save" code

    public ActionResult Create()
    {
        int currMaxSortOrder = session.CreateCriteria(typeof(Section))
                            .SetProjection(Projections.ProjectionList().Add(Projections.Max("Sortorder")))
                            .UniqueResult<int>();
        SectionViewModel sectionViewModel = new SectionViewModel();
        sectionViewModel.Sortorder = currMaxSortOrder + 1;
        return View("Create", "_AdminLayout", sectionViewModel);
    }

    [HttpPost]
    public ActionResult Create(SectionViewModel sectionInputModel)
    {
        if (ModelState.IsValid)
        {
            section = new Section();
            Mapper.Map(sectionInputModel, section);
            using (var tx = session.BeginTransaction())
            {
                session.SaveOrUpdate(section);
        tx.Commit();
            }
            return RedirectToAction("index", "pages").WithFlash(new { success = "Section '" + section.Name + "' was successfully added." });
        }
        return View("Create", "_AdminLayout", section);
    }

Edit 2: Added section entity & mapping

    public class Section : EntityBase
    {
        public virtual String Name { get; set; }
        public virtual int Sortorder { get; set; }
        public virtual String RedirectUrl { get; set; }
        public virtual IList<Page> Pages { get; set; }

        public Section()
        {
            Pages = new List<Page>();
        }

        public virtual void AddPage(Page page)
        {
            page.Section = this;
            this.Pages.Add(page);
        }
    }

    public class SectionMap : EntityBaseMap<Section>
    {
        public SectionMap()
        {
            Map(x => x.Name);
            Map(x => x.Sortorder);
            Map(x => x.RedirectUrl);
            // one to many relationship
            HasMany(x => x.Pages)
                .Inverse()
                .Cascade.All();
        }
    }
}

解决方案

sheepish Doh! moment

(Adding this in case any other n00bs like me run into the same problem)

I finally dug deeper and realized that I had configured it to use AutoMapping while I was creating maps that would only work with FluentMapping. Reverted to use FluentMapping and the Version started working perfectly!

I'm guessing I could possibly use AutoMapping and add a convention that will treat a column named "Version" with CustomType("Timestamp"), but for now am going to use FluentMapping until I get more up to speed.

这篇关于版本作为时间戳在流利的NHibernate / SQL Server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆