views:

488

answers:

1

Hello,

I have a problem figuring out the proper way to get foreign key fields to update using the Entity Framework. I do not have a problem updating them in the database, but rather on the interface.

Here are the relevant tables to my question file:///C:/Users/Mike/Desktop/RelevantTables.bmp

I have a "Team" form with a Master/Detail type view (master = Team, detail = Player), and show the Players in a ListView. When you double click a Player, I show the "Edit Player" form.

Here is how I am loading the data in my TeamForm Window_Loaded event:

var TeamQuery = from t in ScoresDB.Team
    .Include("School").Include("TeamLevel").Include("Player.PlayerPosition")
    .Where(x => x.TeamID == TeamID)
    select t;

TeamData = new TeamCollection(TeamQuery, ScoresDB);                    

TeamViewSource = (CollectionViewSource)FindResource("TeamViewSource");
PlayerViewSource = (CollectionViewSource)FindResource("PlayerViewSource");
TeamViewSource.Source = TeamData;

TeamView = (ListCollectionView)TeamViewSource.View;
TeamView.CurrentChanged += new EventHandler(TeamView_CurrentChanged);

PlayerView = (BindingListCollectionView)PlayerViewSource.View;

This is what I am doing when the user wants to edit a player:

// If the user made changes to the player, then refresh our view
if (PlayerForm.EditPlayer(SelectedPlayer.PlayerID)) {
    ScoresDB.Refresh(System.Data.Objects.RefreshMode.ClientWins, PlayerView);
}

My problem is that, although every field that is not a foreign key does reflect the changes in the ListView, the PlayerPosition does not. It is being properly changed in the database. Do I need to re-query the database every time?

I get the distinct feeling that I am doing all of this quite poorly as I am just starting to trudge through WPF and EF.

If anyone has a clue what is going on or just wishes to tell me how silly I am being doing it this way, that is fine with me!

Thanks in advance, Mike

+1  A: 

I am either completely retarded, way too tired, or both. I vote both! :)

This required exactly one line of code, namely calling the very appropriately named Refresh() method for my BindingListCollectionView (PlayerView).

So I simply change my Edit Player if block as follows:

if (PlayerForm.EditPlayer(SelectedPlayer.PlayerID)) {
    ScoresDB.Refresh(System.Data.Objects.RefreshMode.ClientWins, PlayerView);
    // Added this line and it works
    PlayerView.Refresh();
}

I hope no one spent too much time on this one. I am not totally sure why the non relational fields do update when I simply update the database, but it works now so that's at least something!

Thanks, Mike

mkgrunder