如何扩展ADO.NET实体框架对象与局部类?局部、实体、框架、对象

2023-09-03 03:48:12 作者:妳别 犯贱!

我已经创建了一个包含Toy.edmx,从一个叫玩具数据库生成一个ADO.NET实体数据模型一个Visual Basic WPF应用程序项目。

I've created a Visual Basic WPF Application project that contains Toy.edmx, an ADO.NET Entity Data Model generated from a database called Toy.

及其 Window1.xaml.vb 的文件看起来是这样的:

Its Window1.xaml.vb file looks like this:


1   Class Window1
2   
3       Private Sub Window1_Loaded( _
4       ByVal sender As System.Object, _
5       ByVal e As System.Windows.RoutedEventArgs) _
6       Handles MyBase.Loaded
7   
8           Dim dc As New ToyEntities1
9           Label1.Content = (From c As Client In dc.ClientSet _
10                            Select c).First.FirstName
11  
12      End Sub
13  
14  End Class

这是运行得很好。

但是,如果我添加文件的 Client.vb ...

But, if I add the file Client.vb...


1   Partial Public Class Client
2       Function IsWashington() As Boolean
3           Return Me.LastName = "Washington"
4       End Function
5   End Class

...然后添加一个WHERE子句来我的 Window1.xaml.vb 的查询......

...and add a WHERE clause to my Window1.xaml.vb query...


9           Label1.Content = (From c As Client In dc.ClientSet _
10                            Where c.IsWashington _
11                            Select c).First.FirstName

...然后我得到这个NotSupportedException异常:

...then I get this NotSupportedException:

LINQ到实体不能识别方法布尔IsWashington()方法,这种方法不能被翻译成店前pression。

LINQ to Entities does not recognize the method 'Boolean IsWashington()' method, and this method cannot be translated into a store expression.

如何扩展ADO.NET实体框架与部分类的对象?

How do I extend ADO.NET Entity Framework objects with partial classes?

推荐答案

问题是,你写code,并期待实体框架,以它转换成SQL ...它不能做到这一点。就像LINQ到SQL不能做到这一点。

The problem is that you're writing code, and expecting the Entity Framework to translate that into SQL... it can't do that. Just like LINQ to SQL can't do that.

试想一下,如果你的财产宣读了文件的C:\驱动器...你怎么认为它会处理这个问题? - 不可能

Imagine if your property read a file from the "C:\" drive... how do you think it would handle that? - not possible.