Here we go... I have a table (which unfortunately I can't change) with columns like:
- date
- startTime
- endTime
I have data classes with two fields: startDateTime = date + startTime endDateTime = date + endTime
Map(x => x.EndDateTime)
.Columns.Clear()
.Columns.Add("date", "endTime")
.CustomType<MyCustomType>();
Map(x => x.StartDateTime)
.Columns.Clear()
.Columns.Add("date", "startTime")
.CustomType<MyCustomType>();
MyCustomType is a class implementing IUserType interface. This seemed to work, but it works only with reading data from database. While saving or updating NHibernate puts column "date" twice, and query can not be commited.
My question is: is any way to go around this? I want both fields to be not-read-only. (setting one of them as read-only 开发者_运维知识库helps, but it's not a solution which satisfies me).
Ok, i found a solution... it's not really elegant, but this database is not elegant either:)
So in my BaseController, which handles all stuff like adding, saving, deleting and updating i did some special handling of saving this particular type:
public virtual T Save<T>(T entity)
{
try
{
if (entity.GetType().Equals(typeof(SpecialType)))
{
return SaveSpecialType<T>(entity);
}
ITransaction transaction = session.BeginTransaction();
session.Save(entity);
transaction.Commit();
return entity;
}
catch (Exception ex)
{
if (logError.IsErrorEnabled)
logError.Error(ex.Message + " \n " + ex.InnerException, ex);
throw;
}
}
This SaveSpecialType method executes native sql query inserting that messy type into database
Couple questions. What kind of errors do you get and what would you want to do if the two different properties have different values, even though they are supposed to go to the same underlying table field?
精彩评论