none
entities that have a relations with each other doesn’t update in .net 4 entity framwork RRS feed

  • General discussion

  • entities that have a relations with each other doesn’t update in .net 4 entity framework on time

    i have two class ProductBusinessLayer and UnitBusinessLayer if i comment this line dataGridView1.DataSource = ProductSet; update will be success on time but if i use this line dataGridView1.DataSource = ProductSet; will not be update on time

    i have a two table product and unit product.unitref have a relation with units.id

    //in ProductBusinessLayer
    private static TestDBEntities EntitiesP = new TestDBEntities();        
    private static ObjectSet<Product> ProductSet;
    
    //in UnitBusinessLayer
    private static TestDBEntities EntitiesU = new TestDBEntities();   
    private static ObjectSet<Unit> UnitSet;
    
    private void btnInit_Click(object sender, EventArgs e)
    {
    //in ProductBusinessLayer
    ProductSet = EntitiesP.Products;
    ProductSet.MergeOption = MergeOption.PreserveChanges;
    
    //in UnitBusinessLayer
    UnitSet = EntitiesU.Units;            
    UnitSet.MergeOption = MergeOption.PreserveChanges;
    
    //in GUI
    dataGridView1.DataSource = ProductSet;
    }
    
    private void btnTest_Click(object sender, EventArgs e)
    {
    // produc "wood" use unit cm with id=2
    
    //in UnitBusinessLayer id is unique
    Unit u = UnitSet.Where(p => p.ID == 2  ).FirstOrDefault();
    u.Name = "meter";
    UnitSet.ApplyCurrentValues(u);
    UnitSet.Context.SaveChanges();
    UnitSet.Context.AcceptAllChanges();
    
    //in ProductBusinessLayer
    Product p2 = ProductSet.Where(p => p.Name == "wood").FirstOrDefault();
    string UnitName = p2.Unit.Name;
    if (UnitName.Trim() != u.Name.Trim())
    {
        // UnitName is cm
        MessageBox.Show(" did not Update");
    }
    else
    {
        // UnitName is meter
        MessageBox.Show(" Updated ");
    }                          
    }

    • Changed type s.naderi Sunday, September 9, 2012 7:09 AM
    • Edited by s.naderi Sunday, September 9, 2012 7:12 AM
    Sunday, September 9, 2012 7:07 AM

All replies

  • Hi S.naderi,

    I need some details to analyze your problem.

    Please let me know the steps to reproduce your problem. Click btnInit first, and then click btnTest?

    You want to it is updated on time. Could you please let me know what results should be have and what was the results you got?

    Monday, September 10, 2012 7:03 AM
  • download link

    this is a download link for project and database

    step 1: click init

    step 2: click test for change and test

    Monday, September 10, 2012 11:52 AM