SL4、EF、Ria 服务 - 当服务器端发生或未发生删除时,客户端如何知道发生了什么?

发布于 2024-10-09 15:16:19 字数 1789 浏览 2 评论 0原文

使用 Bill Burrows Intro to SL4 and WCF Ria Services - Golf Examples (http://www.myvbprof.com/MainSite/index.aspx#/zSL4_RIA_01),我不希望你看,但我想我会对于任何希望通过 VB.NET 示例了解 SL4 和 WCF Ria 服务的人来说,将其添加为一个很棒的小教程。

该示例使用客户端和 Web 设置。该数据库有 2 个表:课程和分数。您可以创建新课程,然后为该课程添加分数。数据库设置为不允许您删除分数表中具有相关分数的课程。因此,如果您尝试删除已输入分数的课程,您会收到实体框架错误。

DeleteCourse 子默认情况下如下所示:

Public Sub DeleteCourse(ByVal course As Course)
        If (course.EntityState = EntityState.Detached) Then
            Me.ObjectContext.Courses.Attach(course)
        End If
        Me.ObjectContext.Courses.DeleteObject(course)
End Sub

我通过添加对函数的调用并添加 If/Then 将其修改为如下所示:

Public Sub DeleteCourse(ByVal course As Course)
    Dim myCourseKeyValue As Integer = CheckForScores(course.CourseKey)

    If myCourseKeyValue = 0 Then
        If (course.EntityState = EntityState.Detached) Then
            Me.ObjectContext.Courses.Attach(course)
        End If
        Me.ObjectContext.Courses.DeleteObject(course)
    End If
End Sub

所以在DeleteCourse Sub 的第一行中,我调用了一个名为CheckForScores 的函数,如下所示:

Private Function CheckForScores(ByVal selectedCourseKey As Integer) As Integer
    Using dc As New Golf_DB1Entities
        Dim myScores = (From o In dc.Scores Where o.CourseKey = selectedCourseKey Select o).Count
        Return myScores
    End Using
End Function

所以基本上,我调用CheckForScores 函数来查看所选课程的分数表中是否存在任何条目。这工作正常,我知道可能有一种更简单的方法来获取这些信息,并且我愿意接受建议,但我的实际问题是,因为这是从客户端调用的服务,一旦我发现有所选课程的分数表中的分数条目(来自服务器端),我不会从数据库中删除该记录,但 SL4 应用程序的客户端如何知道删除实际上并未发生?

如果这根本没有任何意义,我很抱歉...

另外,我可以将这个问题标记为 VB.NET 和 C# 吗???,即使从技术上讲这是一个 VB.NET 问题,我知道大多数 SO 用户使用 C#,并且可能可以阅读 VB。如果这被认为是越界,请告诉我,这样我就不会再犯同样的错误了:)。

Using Bill Burrows Intro to SL4 and WCF Ria Services - Golf Example (http://www.myvbprof.com/MainSite/index.aspx#/zSL4_RIA_01), which I don't expect you to look at, but thought I'd add it as a great little tutorial for anyone looking to learn a bit about SL4 and WCF Ria Services with a VB.NET example.

The example uses a Client and Web setup. The database has 2 tables, Courses and Scores. You can create new Courses and then add Scores for that Course. The database is setup to not allow you to delete Courses that have related Scores in the Scores table. So, if you try to delete a Course that you've entered Scores for, you get an Entity Framework error.

The DeleteCourse sub by default looks like this:

Public Sub DeleteCourse(ByVal course As Course)
        If (course.EntityState = EntityState.Detached) Then
            Me.ObjectContext.Courses.Attach(course)
        End If
        Me.ObjectContext.Courses.DeleteObject(course)
End Sub

I modified it to look like this by adding the call to a function, and adding the If/Then:

Public Sub DeleteCourse(ByVal course As Course)
    Dim myCourseKeyValue As Integer = CheckForScores(course.CourseKey)

    If myCourseKeyValue = 0 Then
        If (course.EntityState = EntityState.Detached) Then
            Me.ObjectContext.Courses.Attach(course)
        End If
        Me.ObjectContext.Courses.DeleteObject(course)
    End If
End Sub

So in the first line of the DeleteCourse Sub I'm calling a Function called CheckForScores that looks like this:

Private Function CheckForScores(ByVal selectedCourseKey As Integer) As Integer
    Using dc As New Golf_DB1Entities
        Dim myScores = (From o In dc.Scores Where o.CourseKey = selectedCourseKey Select o).Count
        Return myScores
    End Using
End Function

So basically I'm calling the CheckForScores Function to see if any entries exist in the Scores Table for the selected Course. This works fine, and I know there is probably an easier way to get this information, and I'm open to suggestions, but my actual question is, since this is a service being called from the client, once I figure out that there are Scores entries in the Scores Table for the Selected Course (from the server side), I don't delete the record from the database, but how does the client side of the SL4 application know that the deletion didn't actually take place?

I'm so sorry if this doesn't make any sense at all...

Also, is it OK that I tagged this question as VB.NET and C#???, even though it's technically a VB.NET question, I know that most SO users use C# and can probably read thru the VB. If that's considered out of bounds, let me know so I don't make the same mistake again : ).

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

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

发布评论

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

评论(1

清风无影 2024-10-16 15:16:19

如果我理解正确的话,您有一个子表和一个父表,如果子表中有任何记录,则不会删除父表。这里有两种可能的选择。

一种是只查询两次,一次查看是否可以删除,另一次实际删除。

另一种选择是仅调用服务器端方法。您可以使用 InvokeAttribute 服务器端方法可以有一个返回值,因此您的回调将能够告诉您某些内容是否被删除。谷歌应该有一些如何使用它的例子。

If I understand you right, you have a child table, and a parent table, and you don't delete the parent if there are any records in the child. There are two possible options here.

One would be to just query twice, once to see if you can delete, and another time to actually delete.

Another option is to just call a Server Side Method. You declare a server-side method using the InvokeAttribute A server side method can have a return value, so your callback will be able to tell you if something was deleted or if it wasn't. Google should have some examples of how to use this.

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