1. Refer to this article in the learning process: nettier template generated code framework usage (http://www.cnblogs.com/minwellptl/articles/481208.html)
Here is an example:
1.3.6. Save Association
Deep storage: stores parent objects and subsets at the same time.
/*
Deepsave helper method can help you to save an object and its children in
One call.
*/
Using northwind. dataaccesslayer;
Order order = order. createorder ("alfki", 1, datetime. Now, datetime. Now,
Datetime. Now, 1, 0.1 m, "ship name", "ship address", "Paris", "IDF", "75000 ",
"France ");
Order. orderdetailcollection. Add (order. orderid, 1, 15.6 M, 10, 0.02f );
Order. orderdetailcollection. Add (order. orderid, 2,122.6 M, 43, 0.03f );
Datarepository. orderprovider. deepsave (order );
Console. writeline ("New Order saved: orderid is:" + order. orderid. tostring ());
From the content of the article, it should have been a long time to use, so I changed to the current 2.3.1 method, like this:
Orders orders = orders. createorders ("alfki", 1, datetime. Now, datetime. Now,
Datetime. Now, 1, 0.1 m, "ship name", "ship address", "Paris", "IDF", "75000 ",
"France ");
Orderdetails = new entities. orderdetails ();
Orderdetails. orderid = order. orderid;
Orderdetails. productid = 1;
Orderdetails. unitprice = 15.6 m;
Orderdetails. Quantity = 10;
Orderdetails. Discount = 0.02f;
Orders. orderdetailscollection. Add (orderdetails );
Orderdetails = new entities. orderdetails ();
Orderdetails. orderid = order. orderid;
Orderdetails. productid = 2;
Orderdetails. unitprice = 122.6 m;
Orderdetails. Quantity = 43;
Orderdetails. Discount = 0.03f;
Orders. orderdetailscollection. Add (orderdetails );
// Orders. orderdetailscollection. Add (orders. orderid, 1, 15.6 M, 10, 0.02f ));
// Orders. orderdetailscollection. Add (orders. orderid, 2,122.6 M, 43, 0.03f );
Datarepository. ordersprovider. deepsave (orders );
Console. writeline ("New Order saved: orderid is:" + orders. orderid. tostring ());
However, when executed, the system always prompts "the insert statement conflicts with the foreign key constraint" fk_order_details_orders. This conflict occurs in the Database "northwind", table "DBO. Orders", column 'orderid '. The statement has been terminated ."
After checking, there is indeed such a foreign key, and the orderid is indeed zero (no way, it is actually 0 when the value is assigned), I searched for it, you cannot find a useful link,
There is no way to think about it by yourself, so the tracing starts from deepsave and follows the code below:
If (candeepsave (entity. orderdetailscollection, "list <orderdetails> | orderdetailscollection", deepsavetype, innerlist ))
{
// Update each child parent ID with the real parent ID (mostly used on insert)
Foreach (orderdetails child in entity. orderdetailscollection)
{
If (child. orderidsource! = NULL)
{
Child. orderid = Child. orderidsource. orderid;
}
If (child. productidsource! = NULL)
{
Child. productid = Child. productidsource. productid;
}
}
If (entity. orderdetailscollection. Count> 0 | entity. orderdetailscollection. deleteditems. Count> 0)
{
// Datarepository. orderdetailsprovider. Save (transactionmanager, entity. orderdetailscollection );
Deephandles. Add ("orderdetailscollection ",
New keyvaluepair <delegate, Object> (deepsavehandle <orderdetails>) datarepository. orderdetailsprovider. deepsave,
New object [] {transactionmanager, entity. orderdetailscollection, deepsavetype, childtypes, innerlist}
));
}
}
O, you need it. If it is changed to this, it will be OK:
Orderdetails. orderidsource = orders;
BTW: Why did no one encounter this problem? Strange, isn't it. nettiers is too new, and no one uses this version? However, it has been released for more than a year since 2.3.1. No one will learn it,
No one asked this question. No matter what it is, put it here and save it as a photo. I hope it will help some people.