Link to home
Start Free TrialLog in
Avatar of SilverFox55
SilverFox55Flag for United States of America

asked on

Update Database from RIA Services / Entity Framework without User interface objects.

I'm working on a project that requires Siverlight and RIA Services.  

I am able to retrieve a set of records through RIA services.  I can load them into a data grid, edit them and stuff them back into the database.  Easy enough.  

However, I need to accomplish this behind the scenes without loading the data into a UI component.

I need to cycle through the records in the data set, set some flags based on values in another table and save them.  

I can get it done by using a hidden datagrid and incrementing the SelectedIndex value, but that seems like a hack.  
What is the correct way to do this.  I'm sure it's simple, but I can't find an example.  

Thanks in advance!
 
Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

Create an instance in code for whichever DataContext your ria object usually uses, once you have that instance you can use it to load the data in memory without any grid, once you have manipulated the data you can call DataContext.SubmitChanges() and should persist them in the database.  

I probably would not be able to write a good sample in C# as I usually code in VB, but hopefully it is simple enough.  If you are using a DomainDataSource control in xaml, check the DataContext property to see what type of object it is creating for it.  That is the one you will want to create and load queries with in code.  

I was able to discover the rest using intellisense alone.  Just call YourDataContextObject.Load(YourDataContext.QueryMethod), get the data in the LoadedData event, then manipulate it however you need to and call YourDataContext.SubmitChanges() (there is an event for that when it completes as well, you will need to check the response for errors, and use the e.MarkErrorAsHandled() method if there was an error in either operation).

Best of luck.
Avatar of SilverFox55

ASKER

Yeah - I've been playing around with intellisense and can't find anything that exposes the fields in the entities.  If I bind them to a UI elements, no problem.  Change the value and SubmitChanges(), and I'm done.  I'm just not finding a way to walk through the collection and change the flags.    For instance, I created and loaded a DataContext:

---------------------------------------------------------------------------------------------------
productDomainContext _productDomainContext = new productDomainContext();
_productDomainContext.Load(_productDomainContext.GetNewProductsQuery());
---------------------------------------------------------------------------------------------------

Works great.  If I bind this to a datagrid, I see the expected records.   Now, what I want to do is take a batch number, combine that with a colorcode from the products table and a sequence number to create a serial number for the product.  No user interface is required for the process.  That's where I get stuck.  The code should be something like this:

--------------------------------------------------------------------------------------------------
string batchID = "ABC123";
int batchSeq = 1;
foreach (product in _productDomainContext)
{
    product.serialNumber = String.Concat( batchID,"-", product.colorCode, "-", batchSeq);
    batchSeq++;
}
---------------------------------------------------------------------------------------------------

Of course, if this worked, I wouldn't be asking this question.
When I attempt to compile, I get the error:

--------------------------------------------------------------------------------------------------
" foreach statement cannot operate on variables of type 'namespace.productDomainContext' because namespace.productDomainContext' does not contain a public definition for 'GetEnumerator' "   
--------------------------------------------------------------------------------------------------

I think I can make this work if I can get past this point, but I'm not finding a solution.




ASKER CERTIFIED SOLUTION
Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EE_AutoDeleter
EE_AutoDeleter

I've requested that this question be deleted for the following reason:

                           
No comment has been added to this question in more than 21 days, so it is now classified as abandoned and is now flagged for deletion.


If there is a valid solution, please OBJECT and indicate the comments that are, or would otherwise lead to, a solution.


Use the specific format https:#axxxxxxxx for comment ID(s).


Also, please don't object simply because the author did not respond to your comment. While we understand this is frustrating, unfortunately we cannot force the author to return to the question. Unless you feel you have presented a valid, verifiable solution we'll simply delete the question.


Experts-Exchange Auto Deleter