none
Entity Framework problems RRS feed

  • Question

  • I'm trying to make a Sport Club application. I have classes for League, Team and Player. Teams belong to leagues and players belong to teams. I made forms for creating the entities. From the main form, I call this code:

    using (TeamForm dialog = new TeamForm())
    {
    	if (dialog.ShowDialog() != DialogResult.Cancel)
    	{
    		using (ModelContainer context = new ModelContainer())
    		{
    			context.AddObject(context.TeamSet.EntitySet.Name, dialog.Team);
    			context.SaveChanges();
    			teamsDataGridView.DataSource = context.TeamSet;
    		}
    	}
    }
    
    

    The two problems I have:

    1. When I use a global ModelContainer model context, the last line that sets data source seems like it is not updated - the DataGridView won't display the new data.
    2. When I use local model contexts, I get an error: "The EntityKey property can only be set when the current value of the property is null." (The above version is the one that uses a local context)
    Friday, September 23, 2011 12:10 PM

Answers

  • You're right, that line should read context.TeamSet.AddObject(dialog.Team). Anyway, I have found a workaround - when I change the last line to
    teamsDataGridView.DataSource = from t in context.TeamSet select t;
    
    it works fine with the global context scenario.
    • Marked as answer by Larcolais Gong Wednesday, October 5, 2011 5:08 AM
    Saturday, September 24, 2011 12:17 PM

All replies

  • It's hard from only this little snippet of code. Why do you use AddObject and not the Entity specific function?


    Il saggio è quello che sa di non sapere
    Friday, September 23, 2011 12:28 PM
  • You're right, that line should read context.TeamSet.AddObject(dialog.Team). Anyway, I have found a workaround - when I change the last line to
    teamsDataGridView.DataSource = from t in context.TeamSet select t;
    
    it works fine with the global context scenario.
    • Marked as answer by Larcolais Gong Wednesday, October 5, 2011 5:08 AM
    Saturday, September 24, 2011 12:17 PM