实体模型没有更新的SaveChanges上 [英] Entity Model Not Being Updated on SaveChanges
问题描述
我与实体框架某处一种误解。这code是我的单元测试:
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
我创建一个 PlayerAdmin
,这是一个模型类。 target.Team
是一个实体对象。我要做的就是重置 RosterLink
字段中的团队
只是为了确保我们的测试在同一地点开始了。然后调用 UpdateRosterLink()
功能。这看起来像:
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
当我运行这个code,我可以看到保存到SQL Server从( RosterLink
=名册链接,这拉就像我在单位设置的变化测试)。
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).
不过,我的单元测试失败,因为 team.RosterLink
还是没有
。功能 GetAirForceMensBB()
返回团队
与TeamId = 12434:
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
我敢肯定,我使用的是实体框架的错误,它可能有事可做的事实,我打电话了 PlayerAdminTestManager
在不同的地方,但我不明白为什么。虽然,我将 PlayerAdminManager
是 PlayerAdminTestManager
。 PlayerAdminTestManager
扩展 PlayerAdminManager
,仅供参考。
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.
为什么 team.RosterLink
没有显示从 UpdateRosterLink
?
感谢
修改集装箱
是我的的ObjectContext
。这是我访问存储在数据库中的信息。 Container.Teams
重新presents我的小组
表。
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.
卫生署!
这篇关于实体模型没有更新的SaveChanges上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!