Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Fluent NHibernate保存后不加载跨数据库引用对象_C#_Fluent Nhibernate - Fatal编程技术网

C# Fluent NHibernate保存后不加载跨数据库引用对象

C# Fluent NHibernate保存后不加载跨数据库引用对象,c#,fluent-nhibernate,C#,Fluent Nhibernate,当我第一次调用对象上的get时,跨数据库对象得到了完美的水合作用;但是,当我保存一个对象时,如果CreateUser和UpdateUser为null,则执行一次get操作。OriginalNote引用始终有效(并且它的CreateUser不为null)。我正在尝试在保存后对新ID进行获取,以重新填充整个对象图。原始注释的第一个Get工作得很好,但是填充刚刚保存的对象的Get不工作 映射: public sealed class DocumentNoteMap : EntityMapBase<

当我第一次调用对象上的get时,跨数据库对象得到了完美的水合作用;但是,当我保存一个对象时,如果CreateUser和UpdateUser为null,则执行一次get操作。OriginalNote引用始终有效(并且它的CreateUser不为null)。我正在尝试在保存后对新ID进行获取,以重新填充整个对象图。原始注释的第一个Get工作得很好,但是填充刚刚保存的对象的Get不工作

映射:

public sealed class DocumentNoteMap : EntityMapBase<DocumentNote>
{
    public DocumentNoteMap()
    {
        Table("DocDataNote");
        Id(x => x.DocDataNoteID).GeneratedBy.Native().UnsavedValue(null);

        Map(x => x.DocDataID);
        Map(x => x.DocDataEventID);
        Map(x => x.NoteText).Column("Note");
        References(x => x.OriginalNote).Class<DocumentNote>().Column("ParentDocDataNoteID")
            .Not.LazyLoad()
            .Not.Update();

        References(x => x.UpdateUser).Class<CasUser>().Column("UpdateBy")
            .ReadOnly()
            .Not.LazyLoad()
            .Not.Update();
        References(x => x.CreateUser).Class<CasUser>().Column("CreateBy")
            .ReadOnly()
            .Not.LazyLoad()
            .Not.Update();
    }

public class CasUserMap : ClassMap<CasUser>
{
    public CasUserMap()
    {
        Schema("CAS.dbo");
        Table("CAS_User");
        Id(x => x.CASUserID);

        Map(x => x.FirstName);
        Map(x => x.LastName);
    }
}
公共密封类文档notemap:EntityMapBase
{
公共文档notemap()
{
表(“DocDataNote”);
Id(x=>x.DocDataNoteID).GeneratedBy.Native().UnsavedValue(null);
Map(x=>x.DocDataID);
Map(x=>x.DocDataEventID);
Map(x=>x.NoteText).Column(“Note”);
引用(x=>x.OriginalNote).Class().Column(“ParentDocDataNoteID”)
.Not.LazyLoad()
.Not.Update();
引用(x=>x.UpdateUser.Class().Column(“UpdateBy”)
.ReadOnly()
.Not.LazyLoad()
.Not.Update();
引用(x=>x.CreateUser.Class().Column(“CreateBy”)
.ReadOnly()
.Not.LazyLoad()
.Not.Update();
}
公共类CAUSERMAP:ClassMap
{
公共地图()
{
模式(“CAS.dbo”);
表(“CAS_用户”);
Id(x=>x.serid);
Map(x=>x.FirstName);
Map(x=>x.LastName);
}
}
DocumentNoteRepository:

[Repository]
public class DocumentNoteRepository : HibernateRepositoryBase<DocumentNote,int?>, IDocumentNoteRepository
{
    public DocumentNoteRepository(ISessionFactory sessionFactory)
        :base(sessionFactory)
    {

    }

    public int? Save(DocumentNote entity, bool autoFlush)
    {
        var persisted = (DocumentNote)CurrentSession.Merge(entity);
        entity.DocDataNoteID = persisted.DocDataNoteID;
        if (autoFlush) { CurrentSession.Flush(); }

        return entity.DocDataNoteID;
    }
}
[存储库]
公共类DocumentNoteRepository:HibernateRepositoryBase,IDocumentNoteRepository
{
公共文档NoteRepository(ISessionFactory sessionFactory)
:base(会话工厂)
{
}
公共整数?保存(DocumentNote实体,布尔自动刷新)
{
var persisted=(DocumentNote)CurrentSession.Merge(实体);
entity.DocDataNoteID=persistend.DocDataNoteID;
if(autoFlush){CurrentSession.Flush();}
return entity.DocDataNoteID;
}
}
get方法位于基类中:

/// <summary>
/// Base class for data access operations.
/// </summary>
public abstract class HibernateRepositoryBase<TEntity, TId> : IRepository<TEntity, TId> where TEntity : class
{
    private ISessionFactory _sessionFactory;

    public IQueryable<TEntity> Query()
    {
        return CurrentSession.Query<TEntity>();
    }
    protected HibernateRepositoryBase(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;

    }

    /// <summary>
    /// Session factory for sub-classes.
    /// </summary>
    protected ISessionFactory SessionFactory
    {
        get { return _sessionFactory; }
        private set { _sessionFactory = value; }
    }

    /// <summary>
    /// Get's the current active session. Will retrieve session as managed by the 
    /// Open Session In View module if enabled.
    /// </summary>
    protected ISession CurrentSession
    {
        get
        {
            var session = _sessionFactory.GetCurrentSession();
            _sessionFactory.GetCurrentSession().EnableFilter("TenantFilter").SetParameter("TenantID", User.Current.OrganizationId);
            return session;
        }
    }


    [Transaction(ReadOnly = true)]
    public TEntity Get(TId id)
    {

        var entity=CurrentSession.Get<TEntity>(id);
        if (entity == null)
        {
            throw new DataException("Could not find entity of Type{0} and Primary Key {1}".FormatWith(typeof(TEntity), id.ToString()));   
        }
        // evict so changes don't get inadvertently persisted
        //CurrentSession.Evict(entity);

        return entity;
    }
    [Transaction(ReadOnly = true)]
    public IList<TEntity> GetAll()
    {
        var listOfEntities = CurrentSession.CreateCriteria<TEntity>().List<TEntity>();

        // evict so changes don't get inadvertently persisted - can change when OSIV is removed
        listOfEntities.ToList().ForEach(e => CurrentSession.Evict(e));

        return listOfEntities;
    }
}
//
///用于数据访问操作的基类。
/// 
公共抽象类HibernateRepositoryBase:IRepository其中tenty:class
{
私人ISessionFactory(sessionFactory);;
公共IQueryable查询()
{
返回CurrentSession.Query();
}
受保护的HibernateRepositoryBase(ISessionFactory sessionFactory)
{
_sessionFactory=sessionFactory;
}
/// 
///子类的会话工厂。
/// 
受保护的ISessionFactory会话工厂
{
获取{return\u sessionFactory;}
私有集{u sessionFactory=value;}
}
/// 
///Get是当前活动会话。将检索由管理的会话
///在视图模块中打开会话(如果启用)。
/// 
受保护的会话当前会话
{
得到
{
var session=_sessionFactory.GetCurrentSession();
_sessionFactory.GetCurrentSession().EnableFilter(“TenantFilter”).SetParameter(“TenantID”,User.Current.OrganizationId);
返回会议;
}
}
[事务(只读=真)]
公众购买(TId id)
{
var entity=CurrentSession.Get(id);
if(实体==null)
{
抛出新的DataException(“找不到类型为{0}且主键为{1}的实体”。FormatWith(typeof(tenty),id.ToString());
}
//逐出,这样更改就不会被无意中持久化
//CurrentSession.execute(实体);
返回实体;
}
[事务(只读=真)]
公共IList GetAll()
{
var listOfEntities=CurrentSession.CreateCriteria().List();
//逐出,这样更改就不会被无意中持久化-在删除OSIV时可能会发生更改
ForEach(e=>CurrentSession.execute(e));
返回实体清单;
}
}
编辑:

我尝试过的事情:
1.尝试在保存后(获取前)强制刷新。

2.在保存之后(获取之前)逐出保存的对象我发现了问题。当我试图逐出我的对象时,我逐出了错误的对象。我逐出的是我正在合并的对象(实体),而不是NHibernate在合并中返回的对象(持久化)

收回从合并返回的持久化对象(如下所示)解决了问题

    public int? Save(DocumentNote entity, bool autoFlush)
    {
        var persisted = (DocumentNote)CurrentSession.Merge(entity);
        entity.DocDataNoteID = persisted.DocDataNoteID;
        if (autoFlush)
        {
            CurrentSession.Flush();
            CurrentSession.Evict(persisted);
        }

        return entity.DocDataNoteID;
    }