简体   繁体   English

获取实体框架以更新常规字段和关系字段

[英]Getting Entity Framework to update both normal and relational fields

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 这是我的问题文件的相关表:/// 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. 我有一个具有主要/详细信息类型视图的“团队”表单(主要=团队,详细信息=玩家),并在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: 这是我在TeamForm Window_Loaded事件中加载数据的方式:

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. 我的问题是,尽管不是外键的每个字段都反映了ListView中的更改,但PlayerPosition却没有。 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. 当我刚开始通过WPF和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 预先感谢,迈克

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). 这只需要一行代码,即为我的BindingListCollectionView(PlayerView)调用非常恰当命名的Refresh()方法。

So I simply change my Edit Player if block as follows: 因此,我只需要按以下步骤更改Edit Player即可:

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 谢谢,迈克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM