2012-11-01 28 views
1

所以我已经成功映射了我的整个数据库(所有测试都通过了我的映射)。但是,当我试图实现一些继承映射时,测试不会通过。流利的nHibernate继承映射给出了错误

正常实体总是继承形式包含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' 

在我的其他映射,我没有在我的对象的浮动映射到数据库中的小数点的问题。但是在两个子类中,我确实遇到了问题,因为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")); 
    } 

我试图改变映射到自动映射,然后添加ignorebase或者includebase,但是这并没有改变任何东西。有没有人能够知道我的问题会是什么?

+0

指定'References(x => x.Quotation).Not.LazyLoad()'时,第一个错误消失。 – Firo

回答

1

如在DDL中看到的那样:[insurance_tax] [decimal](18, 0) NOT NULL,该列没有小数位映射,因此3.6变为有效舍入4.使用.Precision(123)来指定要保存的小数位数。

+0

非常感谢您为额外的眼睛。没有看到我忘了设置小数位。 – Quirexx