Fluent NHibernate 映射 - 执行时列名错误

Fluent NHibernate mapping - bad column name while executing

我是 Fluent NHibernate 的新手,我不得不请您提供一些帮助。 我的简单数据库(SQL Server 2017,数据库优先方法)脚本:

CREATE TABLE [dbo].[CDs](
    [IdCD] [int] IDENTITY(1,1) NOT NULL,
    [Artist] [nvarchar](255) NOT NULL,
    [Title] [nvarchar](255) NOT NULL,
    [Tracks] [nvarchar](255) NOT NULL,
    [Box] [nvarchar](255) NOT NULL,
    [Producer] [int] NOT NULL,
    [Year] [int] NOT NULL,
    [Extras] [nvarchar](255) NOT NULL,
    [CreationDate] [datetime2](7) NOT NULL,
 CONSTRAINT [PK__CDs__B77390873411F1EA] PRIMARY KEY CLUSTERED 
(
    [IdCD] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[Producers](
    [IdProducer] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
 CONSTRAINT [PK_Producers2] PRIMARY KEY CLUSTERED 
(
    [IdProducer] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

表之间是一对多的关系。 table 'CDs' 中的 'Producer' 列是 FK,'Producers' table 中的 'IdProducer' 是 PK,它是父级。明确一点 - 一个 'Producer' 可以有多个 'CDs'。 所以我根据网上阅读的许多文章为这 2 table 创建了模型 类:

    public class CDsModel
    {
        public virtual int IdCD { get; set; }
        public virtual string Artist { get; set; }
        public virtual string Title { get; set; }
        public virtual string Tracks { get; set; }
        public virtual string Box { get; set; }
        public virtual int Producer { get; set; }
        public virtual int Year { get; set; }
        public virtual string Extras { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual ProducersModel Producers { get; set; }
    }

    public class ProducersModel
    {
        public virtual int IdProducer { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<CDsModel> CDs { get; set; }
    }

接下来我创建了映射:

public CDsMap()
        {
            Table("CDs");

            Id(x => x.IdCD, "IdCD").GeneratedBy.Identity().UnsavedValue(0);

            Map(x => x.Artist);
            Map(x => x.Title);
            Map(x => x.Tracks);
            Map(x => x.Box);
            Map(x => x.Producer);
            Map(x => x.Year);
            Map(x => x.Extras);
            Map(x => x.CreationDate);
            References(x => x.Producers, "IdProducer");
        }

 public ProducersMap()
        {
            Table("Producers");

            Id(x => x.IdProducer, "IdProducer").GeneratedBy.Identity().UnsavedValue(0);

            Map(x => x.Name);

            HasMany(x => x.CDs).KeyColumn("Producer").AsBag().Not.LazyLoad().Inverse().Cascade.AllDeleteOrphan();
        }

调用controller方法时,session是这样创建的:

    _sessionFactory = Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2012.ConnectionString(ConnectionString).ShowSql)
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProducersModel>())
        .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
        .BuildSessionFactory();

我的问题出现了 - 异常:

NHibernate.Exceptions.GenericADOException: 'could not initialize a collection: [NHApi.Models.ProducersModel.CDs#3][SQL: SELECT cds0_.Producer as producer6_0_1_, cds0_.IdCD as idcd1_0_1_, cds0_.IdCD as idcd1_0_0_, cds0_.Artist as artist2_0_0_, cds0_.Title as title3_0_0_, cds0_.Tracks as tracks4_0_0_, cds0_.Box as box5_0_0_, cds0_.Producer as producer6_0_0_, cds0_.Year as year7_0_0_, cds0_.Extras as extras8_0_0_, cds0_.CreationDate as creationdate9_0_0_, cds0_.IdProducer as idproducer10_0_0_ FROM CDs cds0_ WHERE cds0_.Producer=?]'

内部异常
SqlException: 错误的列名 IdProducer

我尝试在模型、地图和数据库中操作列名,但没有成功。 任何对此问题的帮助将不胜感激。

更多的挖掘和答案真的很简单...... 我找到了这个 post:Fluent NHibernate Many to one mapping

首先我在CDsMap中的对象引用有误。它是:

References(x => x.Producers, "IdProducer");

应该是:

References(x => x.Producers, "Producer");

因为引用应该指向 table 的 FK 字段。

接下来是我的 ProducersModel 中没有构造函数和 List 对象初始化,所以我添加了以下构造函数:

public ProducersModel()
{
    CDs = new List<CDsModel>();
}

但我仍然遇到有关重复字段的异常 "Producer"。最后一步是从我的 CDsModel 和 CDsMap 中删除 "Producer" 字段。