已编辑:已简化" Repository.cs"代码尽可能多。
已编辑2:项目文件:https://drive.google.com/file/d/1kkxQ4bygNDdDTBJTrZBjp1YLyL7YFYQT/view?usp=sharing。
您好
在Visual Studio 2017中,我创建了一种情况,当类名之间存在冲突但未被IntelliSense检测到,但这会导致编译器在LINQ查询中崩溃,因为它试图在错误的命名空间中查询类。
设置" Id"财产,有一个吸气剂:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebSite1.BusinessObjects
{
public class About
{
protected int _id;
public virtual int Id
{
get { return this._id; }
set { this._id = value; }
}
}
}
创建另一个名为" Repository.cs"的类文件。 :
using NHibernate;
using NHibernate.Cfg;
using System;
using System.Threading;
namespace WebSite1.BusinessObjects
{
public class Repository : IDisposable
{
private static ISessionFactory factory;
private static LocalDataStoreSlot mysessions;
private ISession session;
private ITransaction transaction;
private IInterceptor interceptor;
private bool isSessionCreator;
public bool IsOpen
{
get { return Repository.GetIsOpen(this.session); }
}
public ISession Session
{
get { return this.session; }
}
static Repository()
{
Configuration cfg = new Configuration();
cfg.AddAssembly("LogiBox.BusinessObjects");
Repository.factory = cfg.BuildSessionFactory();
Repository.mysessions = Thread.AllocateDataSlot();
}
public Repository()
{
this.isSessionCreator = true;
}
public void Close()
{
if (!this.isSessionCreator)
{
throw new Exception("A gateway that is sharing the session of another gateway cannot be closed directly.");
}
if (this.session == null)
{
return;
}
if (this.session.IsConnected)
{
this.session.Disconnect();
}
if (this.session.IsOpen)
{
this.session.Close();
}
}
public void Dispose(bool isDisposing)
{
if (isDisposing)
{
this.Close();
if (this.transaction != null)
{
this.transaction.Dispose();
this.transaction = null;
}
if (this.session != null)
{
this.session.Dispose();
this.session = null;
}
this.interceptor = null;
if (Thread.GetData(Repository.mysessions) == this)
{
Thread.SetData(Repository.mysessions, null);
}
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private static bool GetIsOpen(ISession session)
{
if (session == null || !session.IsOpen)
{
return false;
}
if (!session.IsConnected)
{
return false;
}
return true;
}
public bool Open()
{
if (!this.isSessionCreator)
{
throw new Exception("A gateway that is sharing the session of another gateway cannot be openned directly.");
}
bool wasOpen = true;
if (this.session == null || !this.session.IsOpen)
{
wasOpen = false;
if (this.interceptor == null)
{
this.session = Repository.factory.OpenSession();
}
else
{
this.session = Repository.factory.OpenSession(this.interceptor);
}
}
if (!this.session.IsConnected)
{
wasOpen = false;
this.session.Reconnect();
}
return wasOpen;
}
public static Repository TRepository()
{
if (Thread.GetData(Repository.mysessions) == null)
{
Thread.SetData(Repository.mysessions, new Repository());
}
return Thread.GetData(Repository.mysessions) as Repository;
}
public static ISession TSession()
{
Repository r = Repository.TRepository();
r.Open();
return r.Session;
}
}
}
将其内容更改为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using NHibernate.Linq;
using WebSite1.BusinessObjects;
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (ISession session = Repository.TSession())
{
List<int> bar = (
from p in session.Query<About>()
select p.Id).ToList();
}
}
}
=&GT;智能感知中没有出现错误 =&GT;构建时,会发生此错误:
CS1061: 'About' does not contain a definition for
'Id' and no extension method 'Id' accepting a first argument of type 'About' could be found (are you missing a using directive or an assembly reference?)
==&GT;这是一个已知的bug吗?当我使用类名时,是否真的必须更具体,因为这种编译器行为?