1

我有一个表 PayrollProcessingProgress,它的主键是另一个表 PayrollPeriod 的 id。它不是一对一的映射,因为 PayrollPeriod 有 1 个或零个 PayrollProcessingProgresses。

我的映射

public class PayrollProcessingProgressMap : ClassMap<PayrollProcessingProgress>
{
public PayrollProcessingProgressMap()
{
    Table("PayrollProcessingProgress");

     Id(x => x.PayrollPeriodId).GeneratedBy.Foreign("PayrollPeriodId");

    Map(x => x.RecordsProcessed);
}
}

餐桌用

CREATE TABLE [dbo].[PayrollProcessingProgress](
    [PayrollPeriodId] [uniqueidentifier] NOT NULL,
    [RecordsProcessed] [int] NOT NULL,
 CONSTRAINT [PK_PayrollProcessingProgress] PRIMARY KEY CLUSTERED 
(
    [PayrollPeriodId] ASC
)
)

GO

ALTER TABLE [dbo].[PayrollProcessingProgress]  WITH CHECK ADD  CONSTRAINT [FK_PayrollProcessingProgress_PayrollPeriods] FOREIGN KEY([PayrollPeriodId])
REFERENCES [dbo].[PayrollPeriods] ([Id])
GO

ALTER TABLE [dbo].[PayrollProcessingProgress] CHECK CONSTRAINT [FK_PayrollProcessingProgress_PayrollPeriods]
GO

我可以读取<>并成功更新我手动保存在数据库中的实体上的数字字段,但是当我尝试

Save(new PayrollProcessingProgress{
PayrollPeriodId = [Guid I know to be a valid payrollperiodid],
RecordsProcessed = 0
}

我收到“无法解析属性:PayrollPeriodId”

4

1 回答 1

1

您必须定义对主实体的引用才能使用 Id 或将其定义为简单的 keyreference

public PayrollProcessingProgress
{
    public virtual PayrollPeriod Period { get; set; }
    public virtual int RecordsProcessed { get; set; }
}

public PayrollProcessingProgressMap()
{
    CompositeId().KeyReference(x => x.Period, "PayrollPeriodId");

    Map(x => x.RecordsProcessed);
}

另一种可能性是将其映射为主类中的可为空属性并使用连接映射。但是,您不能再删除 RecordsProcessed 记录

public PayrollPeriod
{
    ...
    public virtual int? RecordsProcessed { get; set; }
}

public PayrollPeriodMap()
{
    Join("PayrollProcessingProgress", join => 
    {
        join.KeyColumn("PayrollPeriodId");
        join.Optional();
        join.Map(x => x.RecordsProcessed);
    });
}
于 2013-10-01T08:46:25.007 回答