C# Fluent nHibernate继承映射提供错误
因此,我几乎成功地映射了整个数据库(映射的所有测试都通过了)。然而,当我尝试实现一些继承映射时,测试不会通过 普通实体总是从包含Id的类“实体”继承C# Fluent nHibernate继承映射提供错误,c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,因此,我几乎成功地映射了整个数据库(映射的所有测试都通过了)。然而,当我尝试实现一些继承映射时,测试不会通过 普通实体总是从包含Id的类“实体”继承 public class Project : Entity<int> { public virtual string Name { get; set; } public virtual Client Client { get; set; } public virtual Quotation Quotation {
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);
}
}
因此,当我在没有引用子类的情况下验证映射时,测试通过了。但是,当我实现这些子类时,会出现错误
首先是映射:
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");
}
}
我不明白。我不应该在项目对象的报价部分中放置报价的子类吗?因为这就是为什么要实现继承
第二个和第三个映射给出了一个关于错误类型映射的错误:
Lambda_Services_Project.Tests.PersistenceTests.CanCorrectlyMapQuotationPerHour:
System.ApplicationException : For property 'InsuranceTax' of type 'System.Single'
expected '3,6' but got '4'
在我的其他映射中,将对象中的浮点映射到数据库中的十进制并不存在问题。但在这两个子类中,我确实遇到了一个问题,因为float属性显然是添加到数据库的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"));
}
private FluentConfiguration GetConfiguration()
{
流畅地返回。Configure()
.数据库(MsSqlConfiguration.MsSql2008
.ConnectionString(c=>c
.Server(“”)
.数据库(“”)
.Username(“”)
.密码(“”)
.ShowSql())
.Cache(c=>c
.UseQueryCache()
.ProviderClass())
.Mappings(m=>m
.FluentMappings
.AddFromAssemblyOf())
.ExposeConfiguration(x=>x
.SetProperty(“当前会话上下文类”、“线程静态”);
}
我尝试将映射更改为automap,然后添加ignorebase或includebase,但这没有改变任何东西。是否有人可以知道我的问题是什么?如DDL中所示:
[insurance\u tax][decimal](18,0)不为空,
该列映射时没有小数点,并且有效地四舍五入,因此3.6变为4。使用.Precision(123)
指定要保存的小数位数。如DDL中所示:[insurance_tax][decimal](18,0)不为空,
该列映射时没有小数位数,并且有效地四舍五入,因此3.6变为4。使用.Precision(123)
指定要保存的小数位数。当指定引用(x=>x.quote)时,第一个错误消失了。Not.LazyLoad()
?当指定引用(x=>x.quote)时,第一个错误消失了。Not.LazyLoad()
?非常感谢您的额外关注。我没看到我忘了设置小数位。非常感谢你多给我一双眼睛。我没看到我忘了设置小数点。
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'
Lambda_Services_Project.Tests.PersistenceTests.CanCorrectlyMapQuotationPerHour:
System.ApplicationException : For property 'InsuranceTax' of type 'System.Single'
expected '3,6' but got '4'
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"));
}