我有以下 POCO:
[PetaPoco.TableName("Customer")]
[PetaPoco.PrimaryKey("ID")]
public class User
{
[Required(ErrorMessage = "Please enter a Forename")]
[PetaPoco.Column("Name")]
public string Forename { get; set; }
}
public class Product
{
public int Id { get { return 1; } }
public string Name { get { return "TBMaster Licence"; } }
public decimal Price { get { return 358.00M; } } //Ex VAT
[Required(ErrorMessage = "Please enter a valid Machine ID")]
public string MachineId { get; set; }
public int ExpiryElement { get; set; }
public DateTime ExpiryDate
{
get
{
if (ExpiryElement == 0)
{
return new DateTime(1900, 1, 1);
}
else if (ExpiryElement == 1)
{
return DateTime.Now.AddYears(1);
}
else if (ExpiryElement == 2)
{
return DateTime.Now.AddYears(2);
}
else if (ExpiryElement == 3)
{
return DateTime.MaxValue;
}
else
{
return new DateTime(1900, 1, 1);
}
}
}
public bool BloodTestEnabled { get; set; }
public string LicenceKey { get; set; }
public SelectList LicenceOptions
{
get;
set;
}
}
[PetaPoco.TableName("Order")]
[PetaPoco.PrimaryKey("OrderID")]
public class Order
{
[PetaPoco.Column("TxCode")]
public string VendorTxCode { get; set; }
public User WebsiteUser { get; set; }
public List<Product> Products { get; set; }
}
我有以下代码尝试在相应的表中进行插入:
var user = new User();
user.Forename = "fred";
var product = new Product();
product.ExpiryElement = 2;
product.MachineId = "1234";
var order = new Order();
order.VendorTxCode = "1111";
order.WebsiteUser = user;
List<Product> prods = new List<Product>();
prods.Add(product);
order.Products = prods;
var db = new PetaPoco.Database("TBMasterDB");
db.Insert(user);
db.Insert(order);
用户上的插入工作正常,但订单上的插入却不行。当您在 POCO 中有这种关系时,您如何进行插入?这是一种简化的方法,因为我尝试插入的地方只有 Order POCO,那么如何插入到用户、订单和产品表中?