我有两张桌子:
[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();
但它不会插入事务并标记外键约束错误。
有什么方法可以同时添加交易与预订,而无需调用 db.SubmitChanges() 两次来获取 TransactionID?