none
How to store values in multiple tables with EF? RRS feed

  • Question

  • Hello,

    I have a question for which I've searched an answer but I can't seem to find one.I have a combobox which holds the programs for the University and the listbox which displays the courses.

    The combobox in binded to the values "Education" from table Courses and the rest are columns from table RegisterTeacher.What i want to do is to register all the values as one,but I don't know how to make the inner join(because this is what I have to use,I think:D) and save the data into the db.I also have the values in the listbox which are cheked,I don't know how to save the checked value.I have tried with normal query,with linq query.This is the last version that I have of it:

       public void SaveTeacher(object param)
            {
                using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
                {
                    Cours c = new Cours();
                    c.education = education;
                    db.Courses.Add(c);
                    RegisterTeacher s = new RegisterTeacher();
                    if (Checked) { 
                    s.CourseName = courseName;
                    }
                    s.pwd = pwd;
                    s.SNTeacher = SNTeacher;
                    s.UserName = _UserName;
                    db.RegisterTeachers.Add(s);
                    int res = db.SaveChanges();
                    if (res > 0)
                    {
                        MessageBox.Show("Registered succesfully!");
                    }
                    else
                    {
                        MessageBox.Show("Try Again!");
                    }
                }
            }
          

    I am new with MVVM and a baby with linq queries so don't judge too hard.I have a problem uderstanding how I should do he inner join and to save the checked values from the listbox.I'm using db first,MVVM pattern with WPF.Best regards!

    • Moved by CoolDadTx Tuesday, March 20, 2018 5:32 PM EF related
    Tuesday, March 20, 2018 1:55 PM

All replies

  • I am new with MVVM and a baby with linq queries so don't judge too hard.I have a problem uderstanding how I should do he inner join and to save the checked values from the listbox.I'm using db first,MVVM pattern with WPF.Best regards!

    Joins are only used for reading data. You best post to the EF and WPF forums for help. And you also need to understand relationships before proceeding with EF.

    http://www.entityframeworktutorial.net/entity-relationships.aspx

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Tuesday, March 20, 2018 3:00 PM
  • Hi CrazyVibe,

    >>I am new with MVVM and a baby with linq queries so don't judge too hard.I have a problem uderstanding how I should do he inner join and to save the checked values from the listbox

    According to description, I think that it is a one-to-many relationship, which one course maybe has many teachers. and you model like this:

    public class RegisterTeacher
    {
        public int Id { get; set; }
        public string  UserName  { get; set; }
        //other property
        public int CourseId { get; set; }
        public Course Course{ get; set; }
    }
    
    public class Course
    {
        public int CourseId { get; set; }
        public string education { get; set; }   
        public ICollection<RegisterTeacher> RegisterTeachers { get; set; }
    }

    And your add method should be like below:

    public void SaveTeacher(object param)
            {
                using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
                {
                    Cours c = new Cours();
                    c.education = education;
                    //db.Courses.Add(c);
                    RegisterTeacher s = new RegisterTeacher();
                    if (Checked) { 
                    s.CourseName = courseName;
                    }
                    s.pwd = pwd;
                    s.SNTeacher = SNTeacher;
                    s.UserName = _UserName;
                    db.RegisterTeachers.Add(s);
                    int res = db.SaveChanges();
                    s.Course = Cours;
                    if (res > 0)
                    {
                        MessageBox.Show("Registered succesfully!");
                    }
                    else
                    {
                        MessageBox.Show("Try Again!");
                    }
                }
            }
          

    For more information, please refer to:

    http://www.entityframeworktutorial.net/entityframework4.3/add-one-to-many-entity-using-dbcontext.aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 21, 2018 1:40 AM
    Moderator
  • Hello and thank you all for your support.After some debug where everything was ok and the values that I have inserted every time were actually stored in the variables,I thought that I should catch the actual exception.I was surprised when I saw this:

    Truth to be told,I am lost in the logic because my values,once I press submit I can see that are stored at the proper variables,when I debug everything works smoothly without any null values.Although is like this,I get error for the FullName and the CourseName and courseName(which is the checkbox inside the listbox).This is how my binding looks:

     <TextBox HorizontalAlignment="Left" Name="SerialNr" Height="23" Margin="126,13.6,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding SerialNT}" VerticalAlignment="Top" Width="248" Grid.ColumnSpan="2"/>
            <TextBox Grid.ColumnSpan="2" Name="UsernameTeacher" HorizontalAlignment="Left" Height="23" Margin="126,63.6,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding UserName}" VerticalAlignment="Top" Width="248"/>
            <PasswordBox Name="passwordBox" mm:PasswordHelper.BindPassword="true"
                             mm:PasswordHelper.BoundPassword="{Binding Path=Pwd, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"
                    PasswordChar="*"  HorizontalAlignment="Left" Height="27" Margin="126,111.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="248"/>
            <TextBox HorizontalAlignment="Left"  Name="fullName" Height="23" TextWrapping="Wrap"  Text="{Binding FullName}" VerticalAlignment="Top" Width="248" Grid.ColumnSpan="2" Margin="126,158.6,0,0" Grid.Row="1" Grid.RowSpan="2"/>
         
            <ComboBox HorizontalAlignment="Left" DataContextChanged="cbxCourses_DataContextChanged" Text="{Binding Education}" x:Name="cbxCourses" SelectedItem="{Binding Education}" Margin="126,229.2,0,0" 
                     IsSynchronizedWithCurrentItem="True" Grid.Row="1" VerticalAlignment="Top" DisplayMemberPath="education"  Width="228" Grid.RowSpan="2">
         
            </ComboBox>
            <Button Content="Submit" Command="{Binding SaveCommand}"  HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="110" Height="40"/>
            <Button Content="Cancel"  HorizontalAlignment="Left" Margin="361,98.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="111" Height="40"/>
            <ListBox HorizontalAlignment="Left" Name="coursesList"  SelectedItem="{Binding CourseName}" Height="240"   Margin="418,13.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="225" Grid.RowSpan="2"  ItemsSource="{Binding Courses}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="CheckBoxCourses" IsChecked="{Binding Path=Checked,UpdateSourceTrigger=PropertyChanged}" ClickMode="Press" Content="{Binding Path=courseName}" Margin="0"/>
    
    
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Content="Show courses:" Name="ShowCourse"  CommandParameter="{Binding ElementName=cbxCourses,Path=SelectedValue}" Command="{Binding MergeCommand}" HorizontalAlignment="Left" Margin="163,61.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="137" Height="35"/>
        </Grid>

    What could be the problem?Everything seems ok and also one of my colleagues double checked.

    Best regards


    • Edited by CrazyVibe Wednesday, March 21, 2018 9:06 AM
    Wednesday, March 21, 2018 9:05 AM
  • Hi CrazyVibe,

    >>What could be the problem?Everything seems ok and also one of my colleagues double checked.

    Because the code is not complete, I am not sure what cause the issue, could you please share a simple demo, which could reproduce the issue via OneDrive.

    In addition, since it is a new issue, I would suggest that you could post a new thread, it will be beneficial to other communities who have similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 22, 2018 2:58 AM
    Moderator