我是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]
表之间是一对多关系。表“ CD”中的“ Producer”列为FK,“ Producers”表中的“ IdProducer”为PK,它是父级。需要明确的是-一个“生产者”可以有许多“ CD”。 因此,我根据网络上阅读的许多文章为这2个表创建了模型类:
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();
}
在调用控制器方法时,将以以下方式创建会话:
_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=?]' Inner Exception SqlException: Wrong column name IdProducer
我试图在模型,映射和数据库中操纵列名,但是没有运气。 对于此问题的任何帮助将不胜感激。
答案 0 :(得分:1)
更多挖掘和回答非常简单... 我发现了这篇文章:Fluent NHibernate Many to one mapping
首先,我在CDsMap中的对象引用中做错了。是:
References(x => x.Producers, "IdProducer");
它应该是:
References(x => x.Producers, "Producer");
因为引用应该导致表的FK字段。
接下来的事情是我的ProducersModel中没有构造函数和List对象init,因此我添加了以下构造函数:
public ProducersModel()
{
CDs = new List<CDsModel>();
}
但是我仍然对重复的字段“生产者”有一个例外。最后一步是从CDsModel和CDsMap中删除“生产者”字段。