丢失记录ID [英] Losing the record ID
问题描述
稍后的查询我将获得一个记录集,当我展开它时显示代理。这一切都是从记录中获取数据,因为一切都在那里。唯一的问题是当我去抓记录ID时,它总是0,因为它是代理。当我使用记录ID作为选定的值时,这使得构建下拉列表非常困难。更糟糕的是它是随机的。因此,从列表中的5个项目中,其中2个将具有0的ID,因为它们是代理。
我可以使用evict强制它在有时加载。但是,当我需要延迟加载(对于网格)evict是不好的,因为它杀死了惰性负载,我不能动态显示网格内容。
I我使用以下来开始我的会议:
ISession会话= FluentSessionManager.SessionFactory.OpenSession();
session.BeginTransaction();
CurrentSessionContext.Bind(session);
我甚至在我的查询中使用.SetFetchMode(MyTable,Eager) 代理。
代理很好,但我需要记录ID。任何人碰到这个,并有一个简单的修复?
我将不胜感激一些帮助。
每一个请求,这里是我正在运行的查询会导致患者。孩子ID为0,因为它显示为代理 :
public IList< Patients> GetAllPatients()
{
return FluentSessionManager.GetSession()
.CreateCriteria< Patients>()
.Add(Expression.Eq(IsDeleted,false))
.SetFetchMode(Children,Eager)
.List< Patients>();
修复代理问题,你丢失了你的记录ID!
我使用ClearCache来解决这个问题。对于记录结构中的前几个层次来说,这很好。但是,当您有一个Parient.Child.AnotherLevel.OneMoreLevel.DownOneMore的场景,不会修复第4和第5级别。我想出了这个方法。我也确实发现,当我有一对多的时候,大部分都是自我呈现,然后是多对一的映射。所以这里是所有其他人遇到的问题的答案。
public class Parent:DomainBase< int>
{
public virtual int ID {get {return base.ID2; } set {base.ID2 = value; }}
public virtual string Name {get;组; }
....
}
DomainBase:
public abstract class DomainBase< Y>,IDomainBase< Y>
{
public virtual Y ID //一切都有一个身份密钥。
{
get;
set;
}
受保护的内部虚拟Y ID2 //真实身份密钥
{
get
{
Y myID = this.ID;
if(typeof(Y).ToString()==System.Int32)
{
if(int.Parse(this.ID.ToString())== 0)
{
myID = ReadOnlyID;
}
}
返回myID;
}
set
{
this.ID = value;
this.ReadOnlyID = value;
}
}
保护内部虚拟Y ReadOnlyID {get;组; } //真实身份密钥
}
IDomainBase:
public interface IDomainBase< Y>
{
Y ID {get;组; }
}
域映射:
public class ParentMap:ClassMap< Parent,int>
{
public ParentMap()
{
Schema(dbo);
表(父母);
Id(x => x.ID);
Map(x => x.Name);
....
ClassMap:
public class ClassMap< TEntityType,TIdType> :FluentNHibernate.Mapping.ClassMap< TEntityType>其中TEntityType:DomainBase< TIdType>
{
public ClassMap()
{
Id(x => x.ID,ID);
Map(x => x.ReadOnlyID,ID)。ReadOnly();
}
}
I have a record structure where I have a parent record with many children records. On the same page I will have a couple queries to get all the children.
A later query I will get a record set when I expand it it shows "Proxy". That is fine an all for getting data from the record since everything is generally there. Only problem I have is when I go to grab the record "ID" it is always "0" since it is proxy. This makes it pretty tough when building a dropdown list where I use the record ID as the "selected value". What makes this worse is it is random. So out of a list of 5 items 2 of them will have an ID of "0" because they are proxy.
I can use evict to force it to load at times. However when I am needing lazy load (For Grids) the evict is bad since it kills the lazy load and I can't display the grid contents on the fly.
I am using the following to start my session:
ISession session = FluentSessionManager.SessionFactory.OpenSession();
session.BeginTransaction();
CurrentSessionContext.Bind(session);
I even use ".SetFetchMode("MyTable", Eager)" within my queries and it still shows "Proxy".
Proxy is fine, but I need the record ID. Anyone else run into this and have a simple fix?
I would greatly appreciate some help on this.
Thanks.
Per request, here is the query I am running that will result in Patients.Children having an ID of "0" because it is showing up as "Proxy":
public IList<Patients> GetAllPatients()
{
return FluentSessionManager.GetSession()
.CreateCriteria<Patients>()
.Add(Expression.Eq("IsDeleted", false))
.SetFetchMode("Children", Eager)
.List<Patients>();
}
I have found the silver bullet that fixes the proxy issue where you loose your record id! I was using ClearCache to take care of the problem. That worked just fine for the first couple layers in the record structure. However when you have a scenario of Parient.Child.AnotherLevel.OneMoreLevel.DownOneMore that would not fix the 4th and 5th levels. This method I came up with does. I also did find it mostly presented itself when I would have one to many followed by many to one mapping. So here is the answer to everyone else out there that is running into the same problem.
Domain Structure:
public class Parent : DomainBase<int>
{
public virtual int ID { get { return base.ID2; } set { base.ID2 = value; } }
public virtual string Name { get; set; }
....
}
DomainBase:
public abstract class DomainBase<Y>, IDomainBase<Y>
{
public virtual Y ID //Everything has an identity Key.
{
get;
set;
}
protected internal virtual Y ID2 // Real identity Key
{
get
{
Y myID = this.ID;
if (typeof(Y).ToString() == "System.Int32")
{
if (int.Parse(this.ID.ToString()) == 0)
{
myID = ReadOnlyID;
}
}
return myID;
}
set
{
this.ID = value;
this.ReadOnlyID = value;
}
}
protected internal virtual Y ReadOnlyID { get; set; } // Real identity Key
}
IDomainBase:
public interface IDomainBase<Y>
{
Y ID { get; set; }
}
Domain Mapping:
public class ParentMap : ClassMap<Parent, int>
{
public ParentMap()
{
Schema("dbo");
Table("Parent");
Id(x => x.ID);
Map(x => x.Name);
....
}
}
ClassMap:
public class ClassMap<TEntityType, TIdType> : FluentNHibernate.Mapping.ClassMap<TEntityType> where TEntityType : DomainBase<TIdType>
{
public ClassMap()
{
Id(x => x.ID, "ID");
Map(x => x.ReadOnlyID, "ID").ReadOnly();
}
}
这篇关于丢失记录ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!