0

データをロードすると、すべてが正しくマップおよびロードされますが、オブジェクト グラフを で挿入しようとするとPeriodPaymentType例外CalendarEntryがスローされます。

{"Cannot insert the value NULL into column 'PaymentTypeId', table 'CashFlowCalculator.dbo.CalendarEntries'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

理由は、挿入しようとするオブジェクトの外部キー列に値がないためです。マッピングのどこが間違っているのですか?

オブジェクト

public class Period
{
    public virtual int Id { get; private set; }
    public virtual DateTime StartDate { get; set; }
    public virtual DateTime EndDate { get; set; }
    public virtual double OpeningCash { get; set; }
    public virtual IList<CalendarEntry> CalendarEntries { get; set; }

    public Period()
    {
        CalendarEntries = new List<CalendarEntry>();
    }
}

public class PaymentType
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual DateTime EndDate { get; set; }
    public virtual bool IsIncome { get; set; }
    public virtual IList<CalendarEntry> CalendarEntries { get; set; }

    public PaymentType()
    {
        CalendarEntries = new List<CalendarEntry>();
    }
}
public class CalendarEntry
{
    public virtual int Id { get; private set; }
    public virtual double Amount { get; set; }
    public virtual DateTime DueDate { get; set; }
    public virtual PaymentType PaymentType { get; set; }
    public virtual Period Period { get; set; }

    public CalendarEntry(){ }

    public virtual void AddPaymentType(PaymentType paymentType)
    {
        paymentType.CalendarEntries.Add(this);
        PaymentType = paymentType;
    }

    public virtual void AddPeriod(Period period)
    {
        period.CalendarEntries.Add(this);
        Period = period;
    }
}

マッピング

public PeriodMap()
    {
        Table("Periods");
        Id(x => x.Id);
        Map(x => x.OpeningCash);
        Map(x => x.StartDate);
        Map(x => x.EndDate);
        HasMany(x => x.CalendarEntries).LazyLoad().Inverse().Cascade.All();
    }

public PaymentTypeMap()
    {
        Table("PaymentTypes");
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Description);
        Map(x => x.StartDate);
        Map(x => x.EndDate);
        Map(x => x.IsIncome);
        HasMany(x => x.CalendarEntries).LazyLoad().Inverse().Cascade.All();
    }

public CalendarEntryMap()
    {
        Table("CalendarEntries");
        Id(x => x.Id);
        Map(x => x.Amount);
        Map(x => x.DueDate);
        References(x => x.PaymentType).Column("PaymentTypeId");
        References(x => x.Period).Column("PeriodId");
    }
4

2 に答える 2

1

CalendarEntry マッピングで「nullable」 PaymentType プロパティが必要な場合は、次のように指定する必要があると思います。

public CalendarEntryMap()
{
    Table("CalendarEntries");
    Id(x => x.Id);
    Map(x => x.Amount);
    Map(x => x.DueDate);
    References(x => x.PaymentType).Column("PaymentTypeId").Nullable();
    References(x => x.Period).Column("PeriodId");
}

代わりに、null 可能にしたくない場合は、任意の CalendarEntry インスタンスの PaymentType プロパティを手動で設定する必要があります。

于 2011-11-30T13:07:19.410 に答える
0

これは、オブジェクト グラフを設定するために使用するコードです。

Period period = new Period
{
    OpeningCash = 2000,
    StartDate = new DateTime(2011, 1, 1),
    EndDate = new DateTime(2011, 12, 1)
};

PaymentType type = new PaymentType
{
    Description = "Description",
    IsIncome = false,
    Name = "Name",
    StartDate = new DateTime(2010, 11, 23),
    EndDate = new DateTime(2012, 11, 23)
};

CalendarEntry entry = new CalendarEntry 
{
    Amount = 232,
    DueDate = new DateTime(2011, 7, 23)
};
entry.AddPeriod(period);
entry.AddPaymentType(type);
于 2011-11-30T15:26:59.260 に答える