引用错误NHibernate (Error get value from 'IdentitySelectStri

本文关键字:from IdentitySelectStri value get 错误 NHibernate Error 引用 | 更新日期: 2023-09-27 18:06:37

从数据库序列化实体时遇到问题。

项目包含2个类:通知

public class Notification:Entity
{
    public virtual string Title { get; set; }
    public virtual string Text { get; set; }
    public virtual int ToUser { get; set; }
    public virtual int FromUser { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual bool Readed { get; set; }
    public virtual NotificationType Type { get; set; }
    public virtual int DocId { get; set; }
}

和NotificationType

 public class NotificationType:Entity
{
    public virtual string Name { get; set; }
}

其中实体类为

public abstract class Entity
{
    public virtual int Id { get; set; }
}

现在我将它映射到Oracle数据库使用Fluent NHibernate

 public class NotificationMap:ClassMap<Notification>
{
    public NotificationMap()
    {
        Table("NOTIFICATIONS");
        Id(x => x.Id).Column("NOTIFY_ID").Not.Nullable().GeneratedBy.Custom<NHibernate.Id.TriggerIdentityGenerator>(); ;
        Map(x => x.Title).Length(255).Column("TITLE").Nullable().CustomType("AnsiString");
        Map(x => x.Text).Length(255).Column("TEXT").Nullable().CustomType("AnsiString");
        Map(x => x.ToUser).Column("TOUSER_ID").Nullable();
        Map(x => x.FromUser).Column("USER_ID").Nullable();
        Map(x => x.Date).Column("NOTIFY_DATE").Nullable();
        Map(x => x.DocId).Column("TODOC_ID").Nullable();
        Map(x => x.Readed).Column("ISREADED").Nullable();
        References(x => x.Type, "NOTIFY_TYPE").ReadOnly();//.Cascade.None();
    }
}
public class NotificationTypeMap : ClassMap<NotificationType>
{
    public NotificationTypeMap()
    {
        Table("NOTIFICATIONS_TYPE_LIST");
        Id(x => x.Id).Column("NOTIFY_TYPE_ID").Not.Nullable();//.GeneratedBy.Custom<NHibernate.Id.TriggerIdentityGenerator>();
        Map(x => x.Name).CustomType("AnsiString").Length(255).Column("TITLE").Nullable();
    }
}

NHib配置

public static void Configure(string connection)
    {
        _config = Fluently
            .Configure()
            .Database(OracleClientConfiguration
                          .Oracle10
                          .ShowSql()
                          .ConnectionString(connection)
            )
            .Mappings(configuration =>
                      configuration.FluentMappings.AddFromAssemblyOf<DCMap>())
            .ExposeConfiguration(x =>
                                 x.SetInterceptor(new SqlStatementInterceptor()));
               //Build Session factory using configuration
        _sessionFactory = _config.BuildSessionFactory();
    }

创建DB-Tables可以使用生成的SQL脚本

CREATE TABLE NOTIFICATIONS_TYPE_LIST
(
  NOTIFY_TYPE_ID  INTEGER                       NOT NULL,
  TITLE           VARCHAR2(250 BYTE)
)
CREATE TABLE NOTIFICATIONS
(
  NOTIFY_ID    INTEGER                          NOT NULL,
  USER_ID      INTEGER,
  TOUSER_ID    INTEGER,
  NOTIFY_TYPE  INTEGER,
  TITLE        VARCHAR2(250 BYTE),
  TEXT         VARCHAR2(250 BYTE),
  NOTIFY_DATE  DATE,
  TODOC_ID     INTEGER,
  ISREADED     INTEGER,
  ISDELETED    INTEGER
)

并创建表NOTIFICATIONS标识触发器。

现在,我在程序中提取数据:

        IEnumerable<Notification> res;
        using (var unit = UnitOfWork.Create(_dbsession))
        {
            var nRepo = unit.GetRepository<Notification>();
            res = nRepo.GetAllWhere(x=>x.ToUser==User.Id);
            //res is contain list of notification with references types.
        }
        //follow string generate (Error getting value from 'IdentitySelectString' on 'NHibernate.Dialect.Oracle10gDialect'.)
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(res);

在结果中,我捕获异常"错误从'IdentitySelectString'上'NHibernate.Dialect.Oracle10gDialect'获取值"。

有什么问题吗?谢谢。

引用错误NHibernate (Error get value from 'IdentitySelectStri

我通过将映射更改为

解决了这个问题
 References(x => x.Type, "NOTIFY_TYPE").ReadOnly().Not.LazyLoad();