Ask a questionAsk a question
 

AnswerEF 4.0 has still a big problem.

  • Saturday, October 31, 2009 6:57 PMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi
    today i've downloaded vs 2010 beta 2 and create a new simple ef 4 project with northwind database. mainly i got the problem in this thread about object state manager in ef 3.5 sp1, and didn't solved my problem and i expect it will be fix in nex release of ef. but today i follow those steps but i got the same result : my related entity (employee) does not update in order_details dataGridView.
    can anybody help me ? does something wrong ?
    thanks in advance.
    this is my Signature
    • Moved byYichun_FengMSFTTuesday, November 03, 2009 9:11 AM.... (From:ADO.NET Entity Framework and LINQ to Entities)
    •  

Answers

  • Thursday, November 05, 2009 6:41 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello,

    What Zeeshan describes is accurate, but if you are opening a model you created in the first version with VS 2010, you won't automatically get the new FK associations.

    In order to get them, you can either recreate the model making sure there is a check in the option for exposing FKs in the model, or you can modify each association manually this way:

    - On the dependent entity add the FK property and map it to the corresponding property in the store table.
    - Add a referential constraint to the association to indicate what properties in the dependent entity and principal entity must match.
    - Remove the association mapping from the association.

    Besides, looking at your sample code, I still cannot understand what you are accomplishing with the calls to Refresh.

    Looking at the thread that you refer to and also at http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/ca1acf1f-9fe4-463c-b85a-2e1225d6a92e, I cannot see the part in which you store the values selected in the combo boxes in the cells of the grid. I think you would either need to do a lookup to find the right entity and assign it to the navigation property or at least create an EntityKey and set it on the EntityReference.EntityKey property. Can you explain how you do this?

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Friday, November 06, 2009 8:08 PMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Actually, I don't think the general approach explained in that post has that limitation. Changes to the FKs are being recorded each time and you could choose to call SaveChanges only after you have finished with all changes. It is only because the author of the post decided to design the UI in the way he did that you have this perception: he put a single combo box outside the grid to change the customer for the current row in the grid. You could as well have combo box in each row. 

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Tuesday, November 03, 2009 9:09 AMYichun_FengMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Hamed,

     

     

    Thanks for your enthusiasm for EF4.

    I’m moving this thread to EF pre-release forum since there more experts on EF 4.

    After the CTP version released, we'll discuss about it in this forum.
    http://blogs.msdn.com/adonet/archive/2009/10/26/upcoming-ado-net-entity-framework-feature-community-technology-preview-for-visual-studio-2010-beta-2.aspx
     

     

    Best Regards

    Yichun Feng

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Tuesday, November 03, 2009 10:53 AMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Hamed,

     

     

    Thanks for your enthusiasm for EF4.

    I’m moving this thread to EF pre-release forum since there more experts on EF 4.

    After the CTP version released, we'll discuss about it in this forum.
    http://blogs.msdn.com/adonet/archive/2009/10/26/upcoming-ado-net-entity-framework-feature-community-technology-preview-for-visual-studio-2010-beta-2.aspx
     

     

    Best Regards

    Yichun Feng

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Thanks Yichun Feng
    I hope it will be fix as soon as possible. i'm waiting for CTP version.
    this is my Signature
  • Wednesday, November 04, 2009 5:55 AMzeeshan hirani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Don't think u should have this problem any longer. Let me explain some background. In version1, EF used independent association which were tracked separately from the entity itself such that changing a relationship did not change the state of the entity. That relationship was represented as a separate objectstateentry inside the objectstatemanager with isrelationship flag set to true. When you modify the relationship, the prior stateentry was marked as deleted and a new state entry was created in added state. Since most UI reflects to propertychanging and changed event there was no property being changed hence the UI never got updated. 
    With EF4 the default association is foreign key which not only represents the association with a navigation property but also exposes the foreign key column. When you change the relationship using navigation property, the foreign key column also gets effected thus causing the entity state to change and property changing event to fire.

    order.Customer = cust1;
    order.Customer = cust2;

    would cause entity state to change to Modified if your model comprises of foreign key association.

    Zeeshan Hirani
  • Wednesday, November 04, 2009 11:04 AMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Don't think u should have this problem any longer. Let me explain some background. In version1, EF used independent association which were tracked separately from the entity itself such that changing a relationship did not change the state of the entity. That relationship was represented as a separate objectstateentry inside the objectstatemanager with isrelationship flag set to true. When you modify the relationship, the prior stateentry was marked as deleted and a new state entry was created in added state. Since most UI reflects to propertychanging and changed event there was no property being changed hence the UI never got updated. 
    With EF4 the default association is foreign key which not only represents the association with a navigation property but also exposes the foreign key column. When you change the relationship using navigation property, the foreign key column also gets effected thus causing the entity state to change and property changing event to fire.

    order.Customer = cust1;
    order.Customer = cust2;

    would cause entity state to change to Modified if your model comprises of foreign key association.

    Zeeshan Hirani

    Hi Zeeshan Hirani
    in terms of what u told, the objectContext can capture which entity has been changed. hence, this code should be works :

    private void orderBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                this.Validate();
                this.orderDataGridView.EndEdit();
                this.employeeBindingSource.EndEdit();
                this.orderBindingSource.EndEdit();
                this.db.SaveChanges();
    
                foreach (var entry in this.db.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
                {
                    // exclude relationships from stateEntries object (filter only entities)
                    if (entry.Entity != null)
                        this.db.Refresh(System.Data.Objects.RefreshMode.ClientWins, entry.Entity);
                }
                foreach (var entry in this.db.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
                {
                    // exclude relationships from stateEntries object (filter only entities)
                    if (entry.Entity != null)
                        this.db.Refresh(System.Data.Objects.RefreshMode.ClientWins, entry.Entity);
                }
                foreach (var entry in this.db.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
                {
                    // exclude relationships from stateEntries object (filter only entities)
                    if (entry.Entity != null)
                        this.db.Refresh(System.Data.Objects.RefreshMode.ClientWins, entry.Entity);
                }
                this.db.AcceptAllChanges();
                this.db.SaveChanges();
    
                this.Form1_Load(null, null);
            }
    


    but changed data does not reflect to back-end database. for more information, plz see my previous thread about this problem.
    regards
    this is my Signature
  • Thursday, November 05, 2009 6:41 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello,

    What Zeeshan describes is accurate, but if you are opening a model you created in the first version with VS 2010, you won't automatically get the new FK associations.

    In order to get them, you can either recreate the model making sure there is a check in the option for exposing FKs in the model, or you can modify each association manually this way:

    - On the dependent entity add the FK property and map it to the corresponding property in the store table.
    - Add a referential constraint to the association to indicate what properties in the dependent entity and principal entity must match.
    - Remove the association mapping from the association.

    Besides, looking at your sample code, I still cannot understand what you are accomplishing with the calls to Refresh.

    Looking at the thread that you refer to and also at http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/ca1acf1f-9fe4-463c-b85a-2e1225d6a92e, I cannot see the part in which you store the values selected in the combo boxes in the cells of the grid. I think you would either need to do a lookup to find the right entity and assign it to the navigation property or at least create an EntityKey and set it on the EntityReference.EntityKey property. Can you explain how you do this?

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, November 05, 2009 3:47 PMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello,

    What Zeeshan describes is accurate, but if you are opening a model you created in the first version with VS 2010, you won't automatically get the new FK associations.

    In order to get them, you can either recreate the model making sure there is a check in the option for exposing FKs in the model, or you can modify each association manually this way:

    - On the dependent entity add the FK property and map it to the corresponding property in the store table.
    - Add a referential constraint to the association to indicate what properties in the dependent entity and principal entity must match.
    - Remove the association mapping from the association.

    Besides, looking at your sample code, I still cannot understand what you are accomplishing with the calls to Refresh.

    Looking at the thread that you refer to and also at http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/ca1acf1f-9fe4-463c-b85a-2e1225d6a92e, I cannot see the part in which you store the values selected in the combo boxes in the cells of the grid. I think you would either need to do a lookup to find the right entity and assign it to the navigation property or at least create an EntityKey and set it on the EntityReference.EntityKey property. Can you explain how you do this?

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.


    Hi Diego B Vega
    thanks for attention

    i have problem to display Entity.Properties in comboBoxes and save changes back to database when comboxBox's value has been changed.
    assume, we have a orders list from northwind database which display all fields in a seperated controls, for example, display customerID in customerComboBox, i want when change customerComboBox.SelectedValue for current order and save changes, it save changes to back-end database. can u provide an example or tutorial that how to accomplish this ?
    thanks in advance


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
  • Friday, November 06, 2009 12:22 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    You can find some information in the following blog post:

    http://blogs.msdn.com/vsdata/archive/2009/10/16/entity-data-model-working-with-foreign-key-when-you-don-t-have-it.aspx

    It applies to WPF and independent associations (as opposed to FK associations) but many of the same concepts apply, FK associations make it easier.

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Friday, November 06, 2009 7:23 PMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    You can find some information in the following blog post:

    http://blogs.msdn.com/vsdata/archive/2009/10/16/entity-data-model-working-with-foreign-key-when-you-don-t-have-it.aspx

    It applies to WPF and independent associations (as opposed to FK associations) but many of the same concepts apply, FK associations make it easier.

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Hi Diego
    thanks for link
    even though i still do not try this, but it have a big limitation :
    it only works for a single record. i mean it update a single record at the same time and user can not change some values and then click saveChanges.
    ok ?
    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
  • Friday, November 06, 2009 8:08 PMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Actually, I don't think the general approach explained in that post has that limitation. Changes to the FKs are being recorded each time and you could choose to call SaveChanges only after you have finished with all changes. It is only because the author of the post decided to design the UI in the way he did that you have this perception: he put a single combo box outside the grid to change the customer for the current row in the grid. You could as well have combo box in each row. 

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Friday, November 06, 2009 10:07 PMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Actually, I don't think the general approach explained in that post has that limitation. Changes to the FKs are being recorded each time and you could choose to call SaveChanges only after you have finished with all changes. It is only because the author of the post decided to design the UI in the way he did that you have this perception: he put a single combo box outside the grid to change the customer for the current row in the grid. You could as well have combo box in each row. 

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Hi Diego
    yes, that's true, but suppose users can iterate for each order in ordersDataGridView and change some order's customer,after that, click save changes to save all orders which changed it's customer, for this scenario this approach can not help, another problem is that this approach is not straightforward and i think there is a better way to accomplish this. why EF does not handle this automatically ?
    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
  • Friday, November 06, 2009 10:39 PMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, EF manages this automatically and as I said you would only need to put the combo box in the grid, so that a different combo box appears for each row and the user can iterate over the grid and make multiple changes before saving the changes.

    The point of the post I linked is only how to simulate managing foreign keys in .NET 3.5 SP1. This workaround is not necessary anymore in .NET 4.0 because now entities can have real FK properties.

    This is completely orthogonal to the ability of Entity Framework to track changes in multiple entities which has been there since the first version.

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Friday, November 06, 2009 11:25 PMHamed_1983 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, EF manages this automatically and as I said you would only need to put the combo box in the grid, so that a different combo box appears for each row and the user can iterate over the grid and make multiple changes before saving the changes.

    The point of the post I linked is only how to simulate managing foreign keys in .NET 3.5 SP1. This workaround is not necessary anymore in .NET 4.0 because now entities can have real FK properties.

    This is completely orthogonal to the ability of Entity Framework to track changes in multiple entities which has been there since the first version.

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Hi Diego
    i didn't sucess to accomplish this even in vs 2010, plz download and review my sample project and give me a correct solution.
    regards
    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx