使用 NHibernate 和 Fluent 插入 SQL 服务器记录时获取空引用

Getting null reference when inserting SQL Server Record using NHibernate and Fluent

正在尝试使用 nhibernate 和 fluent 将记录插入 table。

创建table的脚本如下

Use [my_lives]
Go

IF Exists(Select * From sys.objects Where object_id = OBJECT_ID(N'[dbo].[tblProfiles]') and Type In (N'U'))
Begin
    Drop Table [dbo].[tblProfiles]
End
Go

CREATE TABLE [dbo].[tblProfiles](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Network] [nvarchar](50) NULL,
    [NetworkId] [nvarchar](50) NULL,
    [Email] [nvarchar](255) NULL,
    [Name] [nvarchar](60) NULL,
    [GivenName] [nvarchar](50) NULL,
    [FamilyName] [nvarchar](50) NULL,
    [Picture] [nvarchar](255) NULL,
    [DOB] [datetime] NULL,
    [RelationshipStatus] [nvarchar](10) NULL,
    [Gender] [nvarchar](10) NULL,
 CONSTRAINT [PK_tblProfiles] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

映射如下;

using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YourLivesDataEngine.DataObjects;

namespace YourLivesDataEngine.Mappings
{
    public class ProfileMap 
        : ClassMap<Profile>
    {
        public ProfileMap()
        {
            Table("tblProfiles");
            Id(x => x.Id).GeneratedBy.Identity(); ;
            Map(x => x.Email);
            Map(x => x.FamilyName);
            Map(x => x.Gender);
            Map(x => x.GivenName);
            Map(x => x.Id);
            Map(x => x.Name);
            Map(x => x.Network);
            Map(x => x.NetworkId);
            Map(x => x.Picture);
        }
    }
}

配置文件对象是这样定义的

public class Profile
{
    public virtual long Id { get; set; }
    public virtual String Network { get; set; }
    public virtual String NetworkId { get; set; }
    public virtual String Email { get; set; }
    public virtual String Name { get; set; }
    public virtual String GivenName { get; set; }
    public virtual String FamilyName { get; set; }
    public virtual String Picture { get; set; }
    public virtual String Gender { get; set; }
}

最后是插入配置文件的代码

    public long Insert ( Profile Profile)
    {
        ISessionFactory factory = null;
        factory = CreateSessionFactory();

        try
        {
            using (var session = factory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(Profile);
                    transaction.Commit();
                }
            }
        }
        catch (Exception e)
        {
            throw e;
        }

        return SelectByNetworkId(Profile.Network,Profile.NetworkId).Id;
    }               // end Insert Procedure

问题是当它到达保存 session.Save(Profile) 时,我得到一个空异常。 None 的属性为空并且会话有一个值,事务也是如此 - 但不明白为什么我会收到空​​异常。

当然,我在做一些愚蠢的事情,因为我已经编写了数百次这样的代码,但看不到它。

错误的堆栈跟踪如下;

   at NHibernate.Engine.EntityKey..ctor(Object identifier, String rootEntityName, String entityName, IType identifierType, Boolean batchLoadable, ISessionFactoryImplementor factory, EntityMode entityMode)
   at NHibernate.Engine.EntityKey..ctor(Object id, IEntityPersister persister, EntityMode entityMode)
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   at YourLivesDataEngine.DataTables.Profiles.Insert(Profile Profile) in C:\Users\Paul\Documents\Visual Studio 2015\Projects\YourLives\YourLivesDataEngine\DataTables\Profiles.cs:line 46

Steven Manuel 是正确的,答案在于映射定义。 ID 出现在对 ID 的调用和对 Map 的调用中。

我进行了一些其他更改以使其正常工作。 NetworkId 在网络中是唯一的,因此使用它。在我添加一些其他网络之前,这是可以的,此时我将需要找到一些方法来定义 NetworkId 和 Network 之间的组合 Id。