使用 ObjectDataProvider 作为绑定源会出现 SQL 错误

发布于 2024-08-09 00:06:46 字数 1088 浏览 4 评论 0原文

我有一个奇怪的问题,我无法找到解决方案:

我制作了一个用 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 技术交流群。

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

发布评论

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

评论(1

独留℉清风醉 2024-08-16 00:06:46

我找到了解决方案:

连接字符串存储为数据库的相对路径,而不是绝对路径。请通过此链接查看更多信息:

嵌入 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

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