?
Solved

Entity Framework object edit/add dialog window

Posted on 2010-09-10
9
Medium Priority
?
311 Views
Last Modified: 2012-05-10
I am using the Entity/Framework and with SQL & LINQ to create a new application.  I have run into a dilemma and after much searching around on the internet I haven’t been able to find the “best practice” that I am looking for.  Here is the situation.

Most of the EF examples keep it pretty simple.  
1)      Put a grid on your form
2)      Bind the grid to a basic linq query
3)      Allow the user to edit the data right in the grid

I am finding that I get into more complex areas of the application I need to add another step to this.  Instead of letting the user edit the data right in the grid, I want to have then double click it (or select it in some other method) and open a dialog window to edit the values for the object.   That would allow for more validation and  a better UI for end users.  

I have done this a couple different ways, usually by passing the EF Context object and the object to edit to the dialog.  I feel as though I am making it harder than it should be.  Does anyone have any good examples of the “best practice” for editing & adding EF objects via dialog windows rather than in the grids themselves?

Thanks in advance..
0
Comment
Question by:MRS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652842
The following sample is done in WPF..

Window2.xaml
public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
            this.DataContext = this;
            Ent = new FinancialsEntities();
        }


        public FinancialsEntities Ent { get; set; }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Window3 wind = new Window3();
            wind.DataContext = this.SelectedItem;
            wind.ShowDialog();
            if (wind.Cancelled)
            {
                Ent.Refresh(System.Data.Objects.RefreshMode.StoreWins, this.SelectedItem);
            }
        }

        public object SelectedItem { get; set; }

        public IQueryable Items
        {
            get
            {
                return from tetmp in Ent.Accounts select tetmp;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Ent.SaveChanges(true);
            this.Close();
        }
    }

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652845
Window2.xaml
<Window x:Class="Tes1234.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="24"/>
        </Grid.RowDefinitions>
        <ListView Name="listView1" SelectedValue="{Binding SelectedItem}" ItemsSource="{Binding Items}">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Edit" Click="MenuItem_Click"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding AccountNo}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding AccountName}"/>
                </GridView>
            </ListView.View>
        </ListView>

        <Button Width="120" Height="24" HorizontalAlignment="Right" Click="Button_Click" Content="Save" Grid.Row="1"/>
    </Grid>
</Window>

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652846
Window3.xaml
<Window x:Class="Tes1234.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window3" Width="350" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="26"/>
            <RowDefinition Height="26"/>
            <RowDefinition Height="26"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="125"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Account No:"/>
        <TextBox Text="{Binding Path=AccountNo}" Grid.Column="1"/>
        
        <TextBlock Text="Account Name:" Grid.Row="1"/>
        <TextBox Text="{Binding Path=AccountName}" Grid.Column="1" Grid.Row="1"/>
        
        <Button Click="Button_Click" Width="75" Height="24" HorizontalAlignment="Right" Content="Ok" Grid.Row="2"/>
        <Button Click="Button_Click_1" Width="75" Height="24" HorizontalAlignment="Right" Content="Cancel" Grid.Row="2" Grid.Column="1"/>
    </Grid>
</Window>

Open in new window

0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652849
Window3.xaml
public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
            this.Cancelled = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Cancelled = false;
            this.Close();
        }

        public bool Cancelled { get; set; }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

Open in new window

0
 

Author Comment

by:MRS
ID: 33661805
I am not super familiar with WPF, what does the datacontext object encapsulate?  Is that a reference to the Context Object?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33663219
It is not the Context object. It just holds the selected item which in this case is Accounts. We are not passing the EntityContext object to the second window but just the Selected Account item.
0
 

Author Comment

by:MRS
ID: 33663344
So in that can how would you go about using that dialog window for creating a new "Account" item.  Would i pass the a new instantiation of an "Account" item to the window and only add it to context object upon a successfull "OK" click?

If the above is the case it sounds like the "best practice" here is to make the dialog windows as dumb as possible and keep all of my hard logic in my parent form.  The dialog window is really only meant to allow the user to change values.  Committing to the database and other logic like that would remain on the parent window.

Does that sound right?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 total points
ID: 33663732
Yes, thats how it is normally done. As you said, for adding a new account, we will create a dummy account and pass it to the second form and upon user confirmation (clicking ok), it gets added to the context. Also, as you have observed, this will make the child form relatively "dumb".
0
 

Author Closing Comment

by:MRS
ID: 33663990
Thank you for taking the time..  
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question