所以我已经成功地映射了几乎我的整个数据库(我的映射的所有测试都通过了)。但是,当我尝试实现一些继承映射时,测试不会通过。
普通实体总是从包含 Id 的类“实体”继承。
public class Project : Entity<int>
{
public virtual string Name { get; set; }
public virtual Client Client { get; set; }
public virtual Quotation Quotation { get; set; }
public virtual IList<HoursSpent> HoursSpent { get; set; }
public Project()
{
HoursSpent = new List<HoursSpent>();
}
public virtual void AddHoursSpent(HoursSpent HourSpent)
{
HourSpent.Project = this;
HoursSpent.Add(HourSpent);
}
}
应映射以下情况(因为我无法发布图像链接):
TABLE [dbo].[project]
[project_id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[client_id] [int] NOT NULL,
[quotation_id] [int] NULL,
PK => project_id
FK => client_id and quotation_id
TABLE [dbo].[quotation](
[quotation_id] [int] IDENTITY(1,1) NOT NULL,
[trainee_cost] [decimal](18, 0) NOT NULL,
[architect_cost] [decimal](18, 0) NOT NULL,
PK => quotation_id
TABLE [dbo].[quotation_per_hour](
[paperwork_expenses] [int] NOT NULL,
[insurance_tax] [decimal](18, 0) NOT NULL,
[hourly_operating_expenses] [decimal](18, 0) NOT NULL,
[quotation_id] [int] NOT NULL,
PK => quotation_id
FK => quotation_id
TABLE [dbo].[quotation_per_percentage](
[quotation_id] [int] NOT NULL,
[wage_percentage] [decimal](18, 0) NOT NULL,
PK => quotation_id
FK => quotation_id
所以当我验证没有引用子类的映射时,测试通过了。但是,当我实现子类时,我得到了错误。
首先是映射:
public class ProjectMap : ClassMap<Project>
{
public ProjectMap()
{
Table("project");
Id(x => x.Id)
.Column("project_id")
.GeneratedBy.Native();
Map(x => x.Name)
.Column("name");
References(x => x.Client)
.Column("client_id")
.Cascade.SaveUpdate();
References(x => x.Quotation)
.Column("quotation_id")
.Cascade.SaveUpdate();
HasMany(x => x.HoursSpent)
.Table("hours_spent")
.KeyColumn("project_id")
.Cascade.SaveUpdate()
.Inverse();
}
}
public class QuotationMap : ClassMap<Quotation>
{
public QuotationMap()
{
Table("quotation");
Id(x => x.Id)
.Column("quotation_id")
.GeneratedBy.Native();
Map(x => x.TraineeCost)
.Column("trainee_cost");
Map(x => x.ArchitectCost)
.Column("architect_cost");
}
}
public class QuotationPerHourMap : SubclassMap<QuotationPerHour>
{
public QuotationPerHourMap()
{
Table("quotation_per_hour");
KeyColumn("quotation_id");
Map(x => x.PaperworkExpenses)
.Column("paperwork_expenses");
Map(x => x.InsuranceTax)
.Column("insurance_tax");
Map(x => x.HourlyOperatingExpenses)
.Column("hourly_operating_expenses");
}
}
public class QuotationPerPercentageMap : SubclassMap<QuotationPerPercentage>
{
public QuotationPerPercentageMap()
{
Table("quotation_per_percentage");
KeyColumn("quotation_id");
Map(x => x.WagePercentage)
.Column("wage_percentage");
}
}
因此,现在作为我的验证,我使用以下 3 种方法,其中每种方法都给我一个错误:
[Test]
public void CanCorrectlyMapProject()
{
Project Project = CreateProject();
var HoursSpent = new List<HoursSpent>()
{
CreateHoursSpent(), CreateHoursSpent()
};
using (var transaction = session.BeginTransaction())
{
new PersistenceSpecification<Project>(session)
.CheckProperty(c => c.Name, Project.Name)
.CheckReference(c => c.Client, Project.Client)
.CheckReference(c => c.Quotation, Project.Quotation)
.CheckList(c => c.HoursSpent, HoursSpent, (c, p) => c.AddHoursSpent(p))
.VerifyTheMappings();
}
}
[Test]
public void CanCorrectlyMapQuotationPerHour()
{
QuotationPerHour Quotation = CreateQuotationPerHour();
using (var transaction = session.BeginTransaction())
{
new PersistenceSpecification<QuotationPerHour>(session)
.CheckProperty(c => c.TraineeCost, Quotation.TraineeCost)
.CheckProperty(c => c.ArchitectCost, Quotation.ArchitectCost)
.CheckProperty(c => c.PaperworkExpenses, Quotation.PaperworkExpenses)
.CheckProperty(c => c.InsuranceTax, Quotation.InsuranceTax)
.CheckProperty(c => c.HourlyOperatingExpenses, Quotation.HourlyOperatingExpenses)
.VerifyTheMappings();
}
}
[Test]
public void CanCorrectlyMapQuotationPerPercentage()
{
QuotationPerPercentage Quotation = CreateQuotationPerPercentage();
using (var transaction = session.BeginTransaction())
{
new PersistenceSpecification<QuotationPerPercentage>(session)
.CheckProperty(c => c.TraineeCost, Quotation.TraineeCost)
.CheckProperty(c => c.ArchitectCost, Quotation.ArchitectCost)
.CheckProperty(c => c.WagePercentage, Quotation.WagePercentage)
.VerifyTheMappings();
}
}
第一个给我:
Lambda_Services_Project.Tests.PersistenceTests.CanCorrectlyMapProject:
System.ApplicationException : For property 'Quotation' expected type
Lambda_Services_Project.Entities.QuotationPerPercentage' but got
Lambda_Services_Project.Entities.Quotation'
我不明白这个。我不应该将引用的子类放在 Project 对象的引用部分吗?因为这就是你要实现继承的原因。
第二个和第三个映射给出了关于错误类型映射的错误:
Lambda_Services_Project.Tests.PersistenceTests.CanCorrectlyMapQuotationPerHour:
System.ApplicationException : For property 'InsuranceTax' of type 'System.Single'
expected '3,6' but got '4'
在我的其他映射中,将对象中的浮点数映射到数据库中的小数点没有问题。但是在这两个子类中我确实遇到了一个问题,因为浮点属性显然是添加到数据库中的 System.Single。
我开始相信问题出在 nfluent Nhibernate 配置上,由于我不太了解配置部分,所以我将其发布在这里:
private FluentConfiguration GetConfiguration()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c
.Server("")
.Database("")
.Username(""))
.Password(""))
.ShowSql())
.Cache(c => c
.UseQueryCache()
.ProviderClass<HashtableCacheProvider>())
.Mappings(m => m
.FluentMappings
.AddFromAssemblyOf<Client>())
.ExposeConfiguration(x => x
.SetProperty("current_session_context_class", "thread_static"));
}
我尝试将映射更改为 automap,然后添加 ignorebase 或 includebase,但这并没有改变任何东西。有没有人可以知道我的问题是什么?