Entity Model Not Being Updated on SaveChanges
I have a misunderstanding somewhere with the Entity Framework. This code is from my unit testing:
Public Sub UpdateRosterLinkTest() Dim target As PlayerAdmin = New PlayerAdmin() target.PlayerAdminManager = playerAdminTestManager target.Team = playerAdminTestManager.GetAirForceMensBB() playerAdminTestManager.resetRosterLink(target) Assert.IsNull(target.Team.RosterLink) Dim playerAdmin As PlayerAdmin = New PlayerAdmin() playerAdmin.TeamId = 12434 playerAdmin.RosterLink = "Roster Link" playerAdmin.UpdateRosterLink() Dim team As DAL.Team = playerAdminTestManager.GetAirForceMensBB() Assert.AreEqual("Roster Link", team.RosterLink) End Sub
I'm creating a PlayerAdmin, which is a model class. target.Team is an Entity object. What I do is reset the RosterLink field in the Team just to make sure our test starts out at the same place. Then I call the UpdateRosterLink() function. That looks like:
Function UpdateRosterLink() As Integer If (PlayerAdminManager Is Nothing) Then PlayerAdminManager = New PlayerAdminManager() End If Team = PlayerAdminManager.GetTeamByTeamId(TeamId) Team.RosterLink = RosterLink Dim numberOfChanges As Integer = PlayerAdminManager.SaveChanges() Return numberOfChanges End Function
When I run this code, I can see the changes saved to the SQL Server this pulls from (RosterLink = Roster Link, like I set in the unit test).
However, my unit test is failing, because team.RosterLink is still Nothing. The function GetAirForceMensBB() returns the Team with TeamId = 12434:
Function GetAirForceMensBB() As DAL.Team Return (From team In Container.Teams Where team.TeamId = 12434).SingleOrDefault End Function
I'm sure I'm using the entity framework incorrectly and it probably has something to do with the fact that I am calling the PlayerAdminTestManager in different places, but I don't understand why. Although, I set the PlayerAdminManager to be the PlayerAdminTestManager. PlayerAdminTestManager extends PlayerAdminManager, fyi.
Why is team.RosterLink not showing the update from UpdateRosterLink?
EDIT Container is my ObjectContext. This is how I access the information stored in the database. Container.Teams represents my Teams table.
The problem was I was referencing different instantiations of the Container (each manager created its own). Thus, the entity items were not attached to anything.