none
EF database add new item from WPF datagrid RRS feed

  • Question

  •  var ctx = new SomeEntities1();
            TblStudentMission smm = new TblStudentMission();
             TblStudentMission sm = e.Row.DataContext as TblStudentMission;
           

      smm.Remark = sm.Remark;                     smm.End_Mission = sm.End_Mission;                     smm.EOD_Mission = sm.EOD_Mission;                     smm.StudentId = sm.StudentId; //???null as sm.staffid is null                     smm.TblMission.Mission_Name = sm.TblMission.Mission_Name; // ???null as tblmissionis null                     sel.ctx1.TblStudentMission.Add(smm);

    tblstudentmission has missionidpk as foreign key from tblmission


    Beautiful Distractions...

    Thursday, February 26, 2015 8:54 AM

Answers

  • You must load the related entities when you assign the ItemsSource property of the DataGrid (https://msdn.microsoft.com/en-us/data/jj574232.aspx?f=255&MSPPError=-2147217396):

    var items = ctx.TblStudentMission.Include("TblStudentMission").ToList(); 
    dataGrid.ItemsSource = items:

    Then you should be able to get the navigation property of the DataContext of the row (sm).

    But as I have already mentioned - please don't ask the same question more than once - this won't affect the new TblStudentMission  object that you are creating any way. You must create the child collection yourself when creating a new object using the new operator:

    var s = new TblStudentMission();
    s.TblMission = new TblMission();
    
    var sm = e.Row.DataContext as TblStudentMission,
    

     Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

    • Marked as answer by Sinandugu Friday, March 20, 2015 12:31 PM
    Tuesday, March 10, 2015 12:49 PM

All replies

  • So what is the problem here?
    Thursday, February 26, 2015 2:49 PM
  • You must load the related entities when you fecth the data from the context using for example eager loading (.Include("TblMission") ). Please refer to the following page for more information: https://msdn.microsoft.com/en-us/data/jj574232.aspx.

    Please remember to close your threads by marking all helpful posts as answer.


    Thursday, February 26, 2015 2:51 PM
  • thanks again

    i used eager loading while binding to itemsource property of the datagrid and it worked fine

    but in the adding new item (roweditending handler) , i use tblstudentmission instance, how do i eager load that since there is no query involved. its rIght from POCO


    Beautiful Distractions...

    Thursday, February 26, 2015 3:15 PM
  • When you simply create a new POCO object using the new operator you must create the child object yourself like this:

    TblStudentMission smm = new TblStudentMission();
    smm.TblMission = new TblMission(); //or whatever your entity type is called.
    smm.TblMission.Mission_Name = "some name";

    Entity Framework is not involved when you create a POCO object this way. It is just like creating any other CLR object and unless you assign a reference type a value it will not referencing any object and thus it will be NULL.


    Please remember to close your threads by marking all helpful posts as answer and please start a new thread if you have a new question.

     

    Thursday, February 26, 2015 3:29 PM
  • Need help on how to approach this..

    adding new item to WPF datagrid, in Roweditending handler,

    i instantiate my entity to the datacontext of datagrid as below

    var s = new TblStudentMission

    var sm = e.Row.DataContext as TblStudentMission;

    problem is TblStudentMission has a foreign column MissionName that needs to be filled

    from datagrid entry but (sm.TblMission.MissionName) comes up as null,

    anyway of making it load. some type of eager loading?

    or any other approach welcome


    Beautiful Distractions...

    Tuesday, March 10, 2015 12:08 PM
  • You must load the related entities when you assign the ItemsSource property of the DataGrid (https://msdn.microsoft.com/en-us/data/jj574232.aspx?f=255&MSPPError=-2147217396):

    var items = ctx.TblStudentMission.Include("TblStudentMission").ToList(); 
    dataGrid.ItemsSource = items:

    Then you should be able to get the navigation property of the DataContext of the row (sm).

    But as I have already mentioned - please don't ask the same question more than once - this won't affect the new TblStudentMission  object that you are creating any way. You must create the child collection yourself when creating a new object using the new operator:

    var s = new TblStudentMission();
    s.TblMission = new TblMission();
    
    var sm = e.Row.DataContext as TblStudentMission,
    

     Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

    • Marked as answer by Sinandugu Friday, March 20, 2015 12:31 PM
    Tuesday, March 10, 2015 12:49 PM