I'm new to Fluent NHibernate and I have to please You about little help. My simple DB (SQL Server 2017, DB first approach) scripts:
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]
Tables are connected with relation one-to-many. 'Producer' column from table 'CDs' is FK and 'IdProducer' from 'Producers' table is PK and it's parent. To be clear - one 'Producer' can have many 'CDs'. So I have created model classes for these 2 tables according to many articles read in web:
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; }
}
Next I have created mappings:
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();
}
When calling controller method, session is created in this way:
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012.ConnectionString(ConnectionString).ShowSql)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProducersModel>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
.BuildSessionFactory();
And my problem appears - exception:
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
I tried to manipulate column names in models, maps and in DB but without luck. Any help with this problem will be appreciated.
More digging and answer was really simple... I have found this post: Fluent NHibernate Many to one mapping
First of all I have done mistake in object reference in CDsMap. It was:
References(x => x.Producers, "IdProducer");
and it should be:
References(x => x.Producers, "Producer");
because reference should lead to FK field of table.
Next thing was no constructor and List object init in my ProducersModel, so I've added following constructor:
public ProducersModel()
{
CDs = new List<CDsModel>();
}
But i still got an aexception about duplicated field "Producer". Last step was removing "Producer" field from my CDsModel and CDsMap.