NHibernate更新未反映在数据库中

时间:2017-03-20 10:27:23

标签: c# nhibernate

我正在尝试使用NHibernate将Message对象的更改保存到数据库,但是虽然我没有收到任何错误消息,但对象的更改不会反映在数据库中。使用SQL事件探查器我没有看到正在数据库上执行的更新查询。 单步执行代码不会显示任何问题或错误,也不会显示日志文件。

在Message类的映射类下面:

[Serializable]
[Class(NameType = typeof(Message), Table = TABLENAME)]
public class Message : IPersistableObject<long?>
{
    public const string TABLENAME = "Message";

    public const string STR_ID = "Id";
    public const string STR_AUTHOR = "Author";
    public const string STR_CONTRACT = "Contract";
    public const string STR_TITLE = "Title";
    public const string STR_TEXT = "Message";
    public const string STR_CREATEDATE = "CreateDate";

    [Id(-2, Name = STR_ID)]
    [Generator(-1, Class = "native")]
    public virtual long? Id { get; set; }

    [ManyToOne(Column = STR_AUTHOR)]
    public virtual User Author { get; set; }

    [ManyToOne (Column = STR_CONTRACT)]
    public virtual Contract Contract { get; set; }

    [Property]
    public virtual string Title { get; set; }

    [Property]
    public virtual string Text { get; set; }

    [Property]
    public virtual DateTime CreateDate { get; set; }
}

在我更新对象的代码下面:

ISession session = SessionFactory.GetCurrentSession();
session.FlushMode = FlushMode.Commit;
using (ITransaction transaction = session.BeginTransaction())
{
    var message = session.Get<Message>(postedMessage.Id);

message.Title = postedMessage.Title;
message.Text = postedMessage.Text;

session.SaveOrUpdate(message);
transaction.Commit();
}
session.FlushMode = FlushMode.Never;

0 个答案:

没有答案