Используя FNH с SQL Server 2008, я пытаюсь добавить версию как метку времени, но сталкиваюсь с ошибкой переполнения SQLDateTime, поскольку значение передается как 01.01.10001 12:00:00. Я обнаружил this (также ссылается здесь ), но все еще испытывает проблему.
// 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");
}
}
Тип данных SQL Server - «дата / время».
Я предполагаю, что это что-то маленькое и глупое, но пока не нашел причину - что мне не хватает?
РЕДАКТИРОВАТЬ: Метод действия для фактического "сохранить" код
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);
}
Редактировать 2: Добавлен раздел сущности и отображение
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();
}
}
}