我有两张桌子:
[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?
答案 0 :(得分:0)
尝试以下方法:
var booking = new Booking();
booking.Transaction.Add(new Transaction { Amount = 200, DatePaid = DateTime.Now });
db.Bookings.InsertOnSubmit(booking);
db.SubmitChanges()