SaveChanges 上实体模型未更新

发布于 2024-12-19 02:50:26 字数 2173 浏览 1 评论 0原文

我对实体框架有一个误解。此代码来自我的单元测试:

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 设置为 PlayerAdminTestManagerPlayerAdminTestManager 扩展了 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

EDIT
Container is my ObjectContext. This is how I access the information stored in the database. Container.Teams represents my Teams table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-12-26 02:50:26

问题是我引用了容器的不同实例(每个管理器创建了自己的实例)。因此,实体项目没有附加到任何东西上。

哎哟!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文