流畅的nHibernate继承映射会产生错误

时间:2012-11-01 18:51:01

标签: c# nhibernate fluent-nhibernate

所以我已经成功地映射了我的整个数据库(我的映射的所有测试都通过了)。但是,当我尝试实现一些继承映射时,测试将不会通过。

普通实体总是从包含Id的“Entity”类继承。

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的数据库中。

我开始相信问题在于nffluent 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,但这并没有改变任何内容。是否有人能够了解我的问题是什么?

1 个答案:

答案 0 :(得分:1)

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