插入具有实体关系的 PetaPoco
我有以下 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,那么如何插入到用户、订单和产品表中呢?
I have the following POCO's:
[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; }
}
I have the below code that tries to do an insert in the respective tables:
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);
The insert on the user works fine but the insert on the order doesn't. How do you do an insert when you have this relationship in your POCO's? This is a simplified approach because where I am trying to do the inserts I only have the Order POCO so how can I insert into the user, order and products table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看了 Gareth Elms 的博客帖子后,我明白了我可以这样做:
本质上你在 Insert 方法中再次映射。不是很漂亮,但它确实有效。希望将来有一种更简洁的方式来做到这一点
After looking at Gareth Elms' blog post I see I can do it like so:
Essentially you mapping again in the Insert method. Not very pretty but it does the trick. Hope there is a tidier way of doing this in the future
我的应用程序中有类似的关系,即;一对多。我会先手动添加订单记录,这样我就有了订单 ID。然后在设置 order.productid 后单独插入每个产品。至于数据库,如果您还没有这样设置,那么在我看来您需要一个 OrderProducts 表来存储订单中的产品。您可能不需要在 OrderProduct 中存储整个产品详细信息,只需存储订单时的产品 ID、描述和金额
I have a similar relationship in my app, ie; one to many. I would manually add the order record first so I have the order id. Then insert each product individually after setting order.productid. As for the database, if you haven't already set it up as such, it looks to me like you need an OrderProducts table that stores the products in an order. You probably don't need to store the whole product details in OrderProduct, just the product id, description and amount at the time of the order