New Posts New Posts RSS Feed: SaveOptions
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

SaveOptions

 Post Reply Post Reply
Author
cuneytgargin View Drop Down
Newbie
Newbie
Avatar

Joined: 05-Feb-2008
Location: Turkey
Posts: 13
Post Options Post Options   Quote cuneytgargin Quote  Post ReplyReply Direct Link To This Post Topic: SaveOptions
    Posted: 16-Nov-2012 at 8:26am
Hi...
I am developing a Windows App.
When I press a button, I create 3 inventory items,
I use Inventory.Create method
like 
public static Inventory Create(string barcode, int fixedAssetID, int departmentID, int locationID, int? supplierID, DateTime receiptDate, decimal? price, int? currencyID, decimal? currentValue, string invoiceNo, DateTime? invoiceDate, int warrantyPeriod, int inventoryStatusID, int? purchaseOrderDetailID, bool checkedOut, int? leasingID, string description, string inventoryKeywords, bool waitingInApprovals, int? PermanentLocationID, string markOrModel, int? insuranceID, int? expensePointID, decimal? currentPrice)
      {
          Inventory aInventory = (Inventory)SessionParameters.Manager.CreateEntity(typeof(Inventory));
          SessionParameters.Manager.GenerateId(aInventory, Inventory.PropertyMetadata.InventoryID);
      aInventory.SomeProperties are set
..
..
}

Then I create 3 InventoryStaffs for each inventory record...
I use InventoryStaff.Create Method like
public static InventoryStaff Create(int inventoryID, int staffID, int? priority, string description, bool? waitingInApprovals)
      {
          InventoryStaff aInventoryStaff = (InventoryStaff)SessionParameters.Manager.CreateEntity(typeof(InventoryStaff));
          SessionParameters.Manager.GenerateId(aInventoryStaff, InventoryStaff.PropertyMetadata.InventoryStaffID);
          aInventoryStaff.InventoryID = inventoryID;
          aInventoryStaff.StaffID = staffID;
...
...
}

then in my code.. 
I use
SaveOptions aSaceOption=new SaveOptions();
Type[] types = new Type[] { typeof(Inventory), typeof(InventoryStaff)};
aSaveOption.PersistenceOrder = types;
try
            {
                SessionParameters.Manager.SaveChanges(aSaveOption);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }


But code drops to Exception saying there is foreign key problem with the two tables...
I change their order in typeof line...
but it did not change...

How will I coordinate Entities saving order ?

Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 16-Nov-2012 at 8:52am
The PersistenceOrder property on SaveOptions is not actually used at this time.   If you look at the Intellisense for it you'll see that we indicate it's for future use.  So that explains why changing the PersisenceOrder has no effect.
 
As to why the save is failing, that's hard to say without more information.  The actual save to the database is handled by the EntityFramework, and EF uses the relationships in the entity model to determine save order.  I would double check which entities are in the EntityManager, and also check the EDMX to see that the associations are set up correctly.  If everything looks good, post the exception message here.
 
 
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 16-Nov-2012 at 11:45am
The most common problem are circular dependencies in your model. Table A has a non-nullable foreign key to table B, and table B has a non-nullable foreign-key to table A. 

If you have a situation like that, the save will always fail, because you have created yourself a chicken or egg problem. You can't insert into table A, because the corresponding record in table B is missing and you can't insert into the table B, because the record in table A is missing, so EF can't determine a working save order.
Back to Top
cuneytgargin View Drop Down
Newbie
Newbie
Avatar

Joined: 05-Feb-2008
Location: Turkey
Posts: 13
Post Options Post Options   Quote cuneytgargin Quote  Post ReplyReply Direct Link To This Post Posted: 19-Nov-2012 at 12:30am
I noticed something...
Even I have a FK relation between Inventory And InventoryInvoicePictures Tables,
I cannot see a relation in EDMX diagram... I think DevForce did not create relation ..
Even I deleted tables from entity model and added again, I still don't see FK relations in model...
Is there any reason, why DevForce cannot add  FK relations...
B.R.
Back to Top
cuneytgargin View Drop Down
Newbie
Newbie
Avatar

Joined: 05-Feb-2008
Location: Turkey
Posts: 13
Post Options Post Options   Quote cuneytgargin Quote  Post ReplyReply Direct Link To This Post Posted: 19-Nov-2012 at 7:20am
I found the reason why EF does not create necessary FK relations..
I had a unique  index on primary key.
And because of that, FK relations were not mapped. It is not a case of DevFoce, but Entity Framework.
B.R.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down