c# - 使用流畅的 Nhibernate 位对属性值进行脱水时出错

标签 c# nhibernate fluent-nhibernate fluent-nhibernate-mapping

我有这门课:

public class Person
{
   public virtual long     ID            { get; set; }
   public virtual string   FirstName     { get; set; }
   public virtual string   LastName      { get; set; }
   public virtual boolean  IsValid       { get; set; }
}

和人员数据映射:

public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.ID);
            Map(x => x.FirstName).Not.Nullable().Length(100);
            Map(x => x.LastName).Not.Nullable().Length(100);
            Map(x => x.IsValid).Not.Nullable();
        }
    }

这是表架构:

CREATE TABLE [dbo].[Person](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](100) NOT NULL,
    [LastName] [nvarchar](100) NOT NULL,    
    [IsValid ] [bit] NOT NULL,
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]

当我使用 Fluent NHibernate 在数据库中插入一个新的 Person 时,我们遇到了这个问题:

error dehydrating property value for `NameSpaceA.IsValid`

以及内部异常:

_innerException = {"Invalid index 2 for this SqlParameterCollection with Count=2."}

最佳答案

正如 Jamie Ide 所说,尝试显式映射由身份生成的 Id,但还要检查并查看从 IsValid 映射中删除 .Not.Nullable() 是否允许其工作。由于在 C# 中,类型是 bool 值,因此无论如何都不能为 null。

public PersonMap()
{
    Id(x => x.ID).GeneratedBy.Identity();
    Map(x => x.FirstName).Not.Nullable().Length(100);
    Map(x => x.LastName).Not.Nullable().Length(100);
    Map(x => x.IsValid);
}

关于c# - 使用流畅的 Nhibernate 位对属性值进行脱水时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15932312/

相关文章:

c# - 表值函数和 NHibernate

nhibernate - 升级到最新的NHibernate和FNH-现在获得SQLite异常“回调例程请求中止…”

c# - 如何使用 SWIG 将 IEnumerable 作为参数的 C++ 委托(delegate)从 C# 创建传递?

c# - 如何在 C# 中创建具有重载算术运算符的接口(interface)?

c# - 在哪里关闭SqlDataReader对象和SqlConnection对象?

c# - Facebook SDK 在 Windows 10 应用程序上加载时出错(XAML、C#)

c# - 在没有刷新/提交的情况下强制执行查询

c# - 流利的休眠状态 : Enum in composite key gets mapped to int when I need string

c# - nhibernate 插入问题 - 有 id 但没有实体对象

nhibernate - 实体没有持久化