如何使用Nhibernate更新行

时间:2014-08-02 21:16:32

标签: c# xml wpf nhibernate sql-server-2012

目标:

根据新数据更新表kund中行的当前和现有数据。新数据位于ContactInfo类中。

问题:

我检索了一条错误消息,上面写着" No persister for:data_layer.ContactInfo"。我尝试找到根问题,但我失败了。我不知道如何解决它。

信息:

  • 不应用新数据行。

  • 我正在使用Nhibernate,VS2013和SQL Server 2012

  • 表格kund中的所有列都不会更新。要更新的列是用xml脚本编写的。

enter image description here

enter image description here

C#代码:

public void Test(ContactInfo pMyContactInfo)
{
    using (var transaction = _session.BeginTransaction())
    {
        _session.SaveOrUpdate(pMyContactInfo);

        transaction.Commit();  
    }

}



namespace data_layer
{
    public class ContactInfo
    {
        public virtual int PK_kund { get; set; }
        public virtual string Kortnummer { get; set; }
        public virtual string Personnummer { get; set; }
        public virtual string Fornamn { get; set; }
        public virtual string Efternamn { get; set; }
        public virtual string Adress { get; set; }
        public virtual string Postnummer { get; set; }
        public virtual string Ort { get; set; }
        public virtual string Datum { get; set; }
        public virtual string Mobilnummer { get; set; }
        public virtual string Hemtelefon { get; set; }
        public virtual string Kon { get; set; }

    }
}

XML:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="data_layer" namespace="data_layer">
  <class name="data_layer.ContactInfo,data_layer" table="Kund">
    <id column="PK_kund" type="Int32" unsaved-value="0">
      <generator class="native">
      </generator>
    </id>
    <property name="Kortnummer" column="Kortnummer" type="string" not-null="true"></property>
    <property name="Personnummer" column="Personnummer" type="string" not-null="true"></property>
    <property name="Fornamn" column="Fornamn" type="string" not-null="true"></property>
    <property name="Efternamn" column="Efternamn" type="string" not-null="true"></property>
    <property name="Adress" column="Adress" type="string" not-null="true"></property>
    <property name="Postnummer" column="Postnummer" type="string" not-null="true"></property>
    <property name="Ort" column="Ort" type="string" not-null="true"></property>
    <property name="Mobilnummer" column="Mobilnummer" type="string" not-null="true"></property>
    <property name="Hemtelefon" column="Hemtelefon" type="string" not-null="true"></property>
    <property name="Kon" column="Kon" type="string" not-null="true"></property>
  </class>
</hibernate-mapping>

T-SQL:

SELECT [PK_kund]
      ,[Kortnummer]
      ,[Personnummer]
      ,[Fornamn]
      ,[Efternamn]
      ,[Adress]
      ,[Postnummer]
      ,[Ort]
      ,[Datum]
      ,[FK_foretag]
      ,[Mobilnummer]
      ,[Hemtelefon]
      ,[Kon]
  FROM [Active_system].[dbo].[Kund]

StackTrace:

NHibernate.MappingException > StackTrace
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName) in p:\nhibernate-core\src\NHibernate\Impl\SessionFactoryImpl.cs:line 473
at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj) in p:\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:line 2789
at NHibernate.Engine.ForeignKeys.IsTransient(String entityName, Object entity, Nullable`1 assumed, ISessionImplementor session) in p:\nhibernate-core\src\NHibernate\Engine\ForeignKeys.cs:line 193
at NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity, String entityName, EntityEntry entry, ISessionImplementor source) in p:\nhibernate-core\src\NHibernate\Event\Default\AbstractSaveEventListener.cs:line 425
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) in p:\nhibernate-core\src\NHibernate\Event\Default\DefaultSaveOrUpdateEventListener.cs:line 64
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) in p:\nhibernate-core\src\NHibernate\Event\Default\DefaultSaveOrUpdateEventListener.cs:line 53
at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) in p:\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:line 2687
at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) in p:\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:line 551
at data_layer.NhibernateDataProvider.Test(ContactInfo pMyContactInfo) in c:\arbete\main_system\data_layer\NhibernateDataProvider.cs:line 271
at usercontrol_kund.Kund_specification_redigering.btn_ok_Click(Object sender, RoutedEventArgs e) in c:\arbete\main_system\usercontrol_kund\Kund_specification_redigering.xaml.cs:line 66
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at System.Windows.Window.ShowDialog()

2 个答案:

答案 0 :(得分:1)

检查您的xml文件是否以.hbm.xml结尾,并在Visual Studio中设置为嵌入式资源。

我敢打赌,这是你的问题;)

答案 1 :(得分:0)

永远不要忘记.hbm.xml和嵌入式资源