我遇到了来自DB的序列化实体的问题。
项目包含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; }
}
现在我使用Fluent NHibernate将其映射到Oracle DB
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 identity-trigger。
现在,我在我的程序中提取数据:
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);
在结果中我捕获异常&#34;从“IdentitySelectString”获取值时出错#39; on&#39; NHibernate.Dialect.Oracle10gDialect&#39;。&#34;
有什么问题?感谢。
答案 0 :(得分:1)
我通过将映射更改为
来解决了我的问题 References(x => x.Type, "NOTIFY_TYPE").ReadOnly().Not.LazyLoad();