Linq to SQL and ObjectDataProvider

Sep 20, 2009 at 3:30 PM

Hello!

I use Linq To SQL to manage data in my database. I use WPF datagrid to display the data binded. I would like to bind data using ObjectDataProvider using the classes that are automatically generated by Linq To SQL class. I can perform the binding very well, but I don't know how to submit changes, inserts and deletions in my WPF datagrid back to my database.I would like to submit changes back to the database by a push of a button.

Usualy when I perform databinding in code behind the control. There a datacontext is created on which submitChanges() can be called. Using XAML and objectDataProvider no such instance of datacontext of my Linq To SQL class is created. So I don't know what object should I call submitChanges() on??.

Here is the XAML code that I have created os far:

ObjectDataProvider:

<xmlns:local="clr-namespace:LunchEye">
<ObjectDataProvider x:Key="scenarijiRegresiranja" ObjectType="{x:Type local:dcLunchEye}" MethodName="GetTable"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:dtScenarijRegre" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider>

DataGrid I would like to perform binding on:

  <my:DataGrid Name="dgvScenariji" Grid.Column="0" Grid.Row="1" Margin="10,0,10,10" Background="White"  
                         ItemsSource="{Binding Source={StaticResource scenarijiRegresiranja}}" AutoGenerateColumns="False">
                <my:DataGrid.Columns>
                    <my:DataGridTextColumn Binding="{Binding Path=idScenarijRegres}" CanUserReorder="False" CanUserResize="False" Header="Id" IsReadOnly="True" />
                    <my:DataGridTextColumn Binding="{Binding Path=naziv}" CanUserReorder="False" CanUserResize="True" Header="Naziv" />
                    <my:DataGridCheckBoxColumn Binding="{Binding Path=aktiven}" CanUserReorder="False" CanUserSort="True" Header="Omogočen" />
                    <my:DataGridTextColumn Binding="{Binding Path=opis}" Header="Opis" />
                </my:DataGrid.Columns>
  </my:DataGrid>

 

Thank you for your help.

 

Best Regards

Jurij

 

 

Oct 12, 2009 at 5:59 PM

In code-behind define two variable:

ListCollectionView defaultView;
LINtoSQLDataContext dataContext;

In constructor or Loaded event asign those variable i to related variable in xaml like this:

defaultView = (ListCollectionView)CollectionViewSource.GetDefaultView(dgvScenariji.ItemsSource);
dataContext = (LINtoSQLDataContext)(FindResource("scenarijiRegresiranja") as ObjectDataProvider).ObjectInstance;

Add new item like the folliwing (Table is type of your lintosql table):

Table newItem = (Table)defaultView.AddNew();
 dataContext.Orders.InsertOnSubmit(newItem);

Save back to database:

defaultView.Refresh();
dataContext.SubmitChanges();

Delete

dataContext.Orders.DeleteOnSubmit((Table)defaultView.CurrentItem);
defaultView.Remove(defaultView.CurrentItem);




Oct 13, 2009 at 6:23 AM

Hi

What kind of object is LINtoSQLDataContext ?

And what about a master-detail scenario (submiting changes to child records) ?

I can't figure out how to do it ?

Thank's

 

 

Oct 13, 2009 at 6:17 PM

@ Bhrnjica: Thank you for your reply! It was very helpful. It did clarify a few things and concepts to me.

@ssandu: LinqToSQL is not an object. It creates a set of classes that map to the tables in your SQL server database. This way every row in each table of the database is represented by an object with propreties that correspond to the columns in the table. Also foreign key relationsships are taken into  account through properties that return a list of objects. LinqToSQL makes data querying and database updating extremely easy. Ever since I discovered LinqToSQL I haven't written a single line of SQL code anymore.

unfortunately I don't know about the master-detail scenario question, since I havn't done it yet. But I think that it shukd be easily done since it is very easy to extract list of orders that belong to a certain customer for example.