如何在Visual Basic中使用LINQ to Entities?

时间:2021-12-08 02:15:55

I've created a .NET solution with two projects:

我创建了一个包含两个项目的.NET解决方案:

  1. ToyData (Visual Basic Class Library)

    ToyData(Visual Basic类库)

  2. ToyOne (Visual Basic WPF Application)

    ToyOne(Visual Basic WPF应用程序)

The ToyData project contains Toy.edmx, an ADO.NET Entity Data Model generated from a database called Toy.

ToyData项目包含Toy.edmx,这是一个从名为Toy的数据库生成的ADO.NET实体数据模型。

The ToyOne project contains this Window1.xaml.vb file:

ToyOne项目包含此Window1.xaml.vb文件:

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

It throws this run-time exception in the auto-generated Toy.Designer.vb file:

它会在自动生成的Toy.Designer.vb文件中抛出此运行时异常:

The specified named connection is either not found in the configuration,  
not intended to be used with the EntityClient provider, or not valid.

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


2  

I've seen this issue before between a service project and a test project (that uses the data objects defined in the service).

我之前在服务项目和测试项目(使用服务中定义的数据对象)之间看到过这个问题。


If you right click on "new ToyEntities" and go to definition, and keep drilling in... you'll get to some auto-generated code that fetches a connection string from a config file.

如果您右键单击“new ToyEntities”并转到定义,并继续钻取...您将获得一些自动生成的代码,该代码从配置文件中获取连接字符串。

Check the ToyData project for a config file. Copy the values into an App.Config file (it may not exist yet) for the other project.

检查ToyData项目以获取配置文件。将值复制到另一个项目的App.Config文件(它可能尚不存在)中。

#1


2  

I've seen this issue before between a service project and a test project (that uses the data objects defined in the service).

我之前在服务项目和测试项目(使用服务中定义的数据对象)之间看到过这个问题。


If you right click on "new ToyEntities" and go to definition, and keep drilling in... you'll get to some auto-generated code that fetches a connection string from a config file.

如果您右键单击“new ToyEntities”并转到定义,并继续钻取...您将获得一些自动生成的代码,该代码从配置文件中获取连接字符串。

Check the ToyData project for a config file. Copy the values into an App.Config file (it may not exist yet) for the other project.

检查ToyData项目以获取配置文件。将值复制到另一个项目的App.Config文件(它可能尚不存在)中。