SaveChanges 上实体模型未更新
我对实体框架有一个误解。此代码来自我的单元测试:
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
我正在创建一个 PlayerAdmin
,它是一个模型类。 target.Team
是一个实体对象。我所做的就是重置 Team
中的 RosterLink
字段,以确保我们的测试在同一位置开始。然后我调用 UpdateRosterLink()
函数。看起来像:
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
当我运行此代码时,我可以看到保存到从中提取的 SQL Server 的更改(RosterLink
= Roster Link,就像我在单元测试中设置的那样)。
但是,我的单元测试失败了,因为 team.RosterLink
仍然 Nothing
。函数 GetAirForceMensBB()
返回 TeamId = 12434 的 Team
:
Function GetAirForceMensBB() As DAL.Team
Return (From team In Container.Teams Where team.TeamId = 12434).SingleOrDefault
End Function
我确定我错误地使用了实体框架,这可能与以下事实有关:我在不同的地方调用 PlayerAdminTestManager
,但我不明白为什么。尽管如此,我将 PlayerAdminManager
设置为 PlayerAdminTestManager
。 PlayerAdminTestManager
扩展了 PlayerAdminManager
,仅供参考。
为什么 team.RosterLink
不显示来自 UpdateRosterLink
的更新?
谢谢
编辑 Container
是我的ObjectContext
。这就是我访问数据库中存储的信息的方式。 Container.Teams
代表我的 Teams
表。
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
?
Thanks
EDITContainer
is my ObjectContext
. This is how I access the information stored in the database. Container.Teams
represents my Teams
table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我引用了容器的不同实例(每个管理器创建了自己的实例)。因此,实体项目没有附加到任何东西上。
哎哟!
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.
Doh!