私は2つのテーブルを持っています:
[Table(Name="Booking")]
public class Booking
{
private EntityRef<Transaction> _transaction;
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID;
[Column] public int Quantity;
[Column] public int EventID;
[Column] public int TransactionID;
[Association(Strorage = "_transaction", ThisKey = "TransactionID", OtherKey = "ID")]
public Transaction Transaction
{
get { return _transaction.Entity; }
set { _transaction.Entity = value; }
}
}
[Table(Name="Transaction")]
public class Transaction
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID;
[Column] public decimal Amount;
[Column] public DateTime DatePaid;
}
私は次のことをしたい:
Booking booking = new Booking
{
/* set data */
Transaction = new Transaction
{
Amount = 200,
DatePaid = DateTime.Now
}
}
db.Bookings.InsertOnSubmit(booking);
db.SubmitChanges();
ただし、トランザクションは挿入されず、外部キー制約エラーがフラグされます。
TransactionID を取得するために db.SubmitChanges() を 2 回呼び出さずに、同時に予約にトランザクションを追加する方法はありますか?