使用 ObjectDataProvider 作为绑定源会出现 SQL 错误
我有一个奇怪的问题,我无法找到解决方案:
我制作了一个用 C# 编写的小 WPF 应用程序,它使用附加的 SQL Server Express .mdf 数据库,然后我通过 LINQ 对其进行操作。
我在表单上有一个 ListView,其 datacontext 设置为 .DataContext = dr.FindAllBuyOrders()
,它返回 IQueryable BuyOrder 对象。这一切都很好。但是,当我通过 xaml 使用 ObjectDataProvider 执行相同操作时:
<ObjectDataProvider MethodName="FindAllBuyOrders" ObjectType="{x:Type local:DataRepository}" x:Key="dataBuyOrders" />
<ListView Name="listViewBuyOrders" VerticalContentAlignment="Top" ItemsSource="{Binding Source={StaticResource dataBuyOrders}}" ItemTemplate="{StaticResource listViewBuyOrders}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
然后出现以下错误:
An attempt to attach an auto-named database for file
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Data.mdf
failed. A database with the same name exists, or specified file
cannot be opened, or it is located on UNC share.
I have a weird problem that i can't figure out a solution for:
I've made a little WPF app written in C# that uses an attached SQL Server Express .mdf database which i then manipulates through LINQ.
I have a ListView on the form which datacontext is set to .DataContext = dr.FindAllBuyOrders()
which returns an IQueryable BuyOrder object. This all works fine. however, when i do the same through xaml with the ObjectDataProvider:
<ObjectDataProvider MethodName="FindAllBuyOrders" ObjectType="{x:Type local:DataRepository}" x:Key="dataBuyOrders" />
<ListView Name="listViewBuyOrders" VerticalContentAlignment="Top" ItemsSource="{Binding Source={StaticResource dataBuyOrders}}" ItemTemplate="{StaticResource listViewBuyOrders}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
then i get the following error:
An attempt to attach an auto-named database for file
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Data.mdf
failed. A database with the same name exists, or specified file
cannot be opened, or it is located on UNC share.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案:
连接字符串存储为数据库的相对路径,而不是绝对路径。请通过此链接查看更多信息:
嵌入 SQL Express 以与 Linq to SQL 和用户实例一起使用可能会很痛苦
I found the solution:
The connectionstring was stored with a relative path to the database instead of the absolute one. See more at this link:
Embedding SQL Express to use with Linq to SQL and User Instances can be a pain