Print Page | Close Window

SaveOptions

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3805
Printed Date: 12-Mar-2025 at 6:32pm


Topic: SaveOptions
Posted By: cuneytgargin
Subject: SaveOptions
Date 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 ?




Replies:
Posted By: kimj
Date 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.
 
 


Posted By: mgood
Date 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.


Posted By: cuneytgargin
Date 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.


Posted By: cuneytgargin
Date 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.



Print Page | Close Window