简体   繁体   English

NHibernate BasicSample与Nhibernate 3.3

[英]NHibernate BasicSample with Nhibernate 3.3

I started a new project and for this project i wanted to use BasicSample (NHibernate best practise, http://www.codeproject.com/Articles/13390/NHibernate-Best-Practices-with-ASP-NET-1-2nd-Ed ) i haven´t used this before and now i get problems. 我开始了一个新项目,对于这个项目,我想使用BasicSample(NHibernate最佳实践, http://www.codeproject.com/Articles/13390/NHibernate-Best-Practices-with-ASP-NET-1-2nd-Ed )我以前没有使用过,现在遇到了问题。 In this project i use NHibernate 3.3.1 and modified the source code from BasicSample to use my entieties. 在这个项目中,我使用NHibernate 3.3.1,并从BasicSample修改了源代码以使用我的实体。 I get this error for every class model (just showing for the class "Month"): 对于每个类模型,我都会收到此错误(仅为类“ Month”显示):

System.TypeInitializationException was unhandled by user code

Message=The type initializer for 'Nested' threw an exception. Message =“嵌套”的类型初始值设定项引发了异常。 Source=Project.Data TypeName=Nested StackTrace: at Project.Data.NHibernateSessionManager.get_Instance() in Project.Data\\NHibernateSessionManager.cs:line 28 at Project.Web.NHibernateSessionModule.BeginTransaction(Object sender, EventArgs e) in App_Code\\NHibernateSessionModule.cs:line 27 at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) InnerException: NHibernate.InvalidProxyTypeException Message=The following types may not be used as proxies: Project.Core.Domain.Calendar.Month: method Equals should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month: method IsTransient should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month: method get_Year should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month: met Source = Project.Data TypeName = Nested StackTrace:位于Project.Data \\ NHibernateSessionManager.cs中Project.Data.NHibernateSessionManager.get_Instance()处:App_Code \\ NHibernateSessionModule中Project.Web.NHibernateSessionModule.BeginTransaction(Object sender,EventArgs e)处的第28行.cs:System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()位于System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值,并且已同步完成)处的InnerException:NHibernate.InvalidProxyTypeException Message =以下类型不能用作代理:Project.Core.Domain.Calendar.Month:方法等于应为“公共/受保护的虚拟”或“受保护的内部虚拟” Project.Core.Domain.Calendar.Month:方法IsTransient应为“ public /受保护的虚拟”或“受保护的内部虚拟” Project.Core.Domain.Calendar.Month:方法get_Year应为“公共/受保护的虚拟”或“受保护的内部虚拟” Project.Core.Domain.Calendar.Month:已满足 hod set_Year should be 'public/protected virtual' or 'protected internal virtual' Blockquote hod set_Year应该是“公共/受保护的虚拟”或“受保护的内部虚拟”

the class looks like: 该类看起来像:

public class Month : DomainObject<int>
{
    private Year _year;
    public Year Year
    {
        get { return _year; }
        set { _year = value; }
    }

    private string _monthName; 
    public string MonthName
    {
        get { return _monthName; }
        set { _monthName = value; }
    }

    private IList<Consumption> consumptions = new List<Consumption>();
    public IList<Consumption> Consumptions
    {
        get { return new List<Consumption>(consumptions).AsReadOnly(); }
        protected set { consumptions = value; }
    }

    public Month()
    { }

    public override int GetHashCode()
    {
        return (GetType().FullName + "|" +
                MonthName.GetHashCode()).GetHashCode();
    }

Does anoyone know what the problem could be? Anoyone知道可能是什么问题吗? I have been crosschecking my project and the BasicSample project but i can´t find anything that missmatch. 我一直在交叉检查我的项目和BasicSample项目,但找不到任何不匹配的东西。

2.Can someone explain for me how to use the GetHashCode from DaomainObject because i don´t know how to use this one in every class= Should every property in the class be added here? 2.有人可以帮我解释一下如何使用DaomainObject的GetHashCode吗,因为我不知道如何在每个类中使用此代码=应该在此处添加类中的每个属性吗?

I hope someone can help me. 我希望有一个人可以帮助我。

The problem is contained in your exception stack trace. 该问题包含在您的异常堆栈跟踪中。 You need to read them more carefully: 您需要更仔细地阅读它们:

The following types may not be used as proxies: 以下类型不能用作代理:
Project.Core.Domain.Calendar.Month: method Equals should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month:方法等于应为“公共/受保护的虚拟”或“受保护的内部虚拟”
Project.Core.Domain.Calendar.Month: method IsTransient should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month:方法IsTransient应该是“公共/受保护的虚拟”或“受保护的内部虚拟”
Project.Core.Domain.Calendar.Month: method get_Year should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month:方法get_Year应该是“公共/受保护的虚拟”或“受保护的内部虚拟”
Project.Core.Domain.Calendar.Month: method set_Year should be 'public/protected virtual' or 'protected internal virtual' Project.Core.Domain.Calendar.Month:方法set_Year应该是“公共/受保护的虚拟”或“受保护的内部虚拟”

What it says is basically, that you need to make all public and protected members of your class virtual , otherwise lazy loading won't work. 它的基本含义是,您需要使类的所有public成员和protected成员成为virtual成员,否则延迟加载将无法进行。


GetHashCode : GetHashCode
The hash code of an instance should not change over the course of its lifetime, therefore you shouldn't include properties that can be changed. 实例的哈希码在其生命周期内不应更改,因此,您不应包含可以更改的属性。

  1. NHibernate uses lazy loading by default. NHibernate默认使用延迟加载。 It performs it by creating proxy classes from your entities - inheriting from your entity and overriding it's members. 它通过从您的实体创建代理类来执行它-从您的实体继承并覆盖其成员。 In order for this to work, you need to mark all of your entity members as virtual . 为了使它起作用,您需要将所有实体成员标记为virtual That was what it complains about in exception message. 这就是它在异常消息中所抱怨的。

  2. As for GetHashCode , you should implement it in DomainObject and only use Id in GetHashCode overrides. 至于GetHashCode ,您应该在DomainObject实现它,并且只能在GetHashCode替代中使用Id You should also override Equals . 您还应该覆盖Equals Here you can find reasons why: NHibernate: Reasons for overriding Equals and GetHashCode . 在这里,您可以找到原因: NHibernate:重写Equals和GetHashCode的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM