使用ObjectDataProvider作为绑定源会产生SQL错误

时间:2009-10-24 11:22:13

标签: wpf xaml sql-server-express objectdataprovider

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

我用C#编写了一个小的WPF应用程序,该应用程序使用附加的SQL Server Express .mdf数据库然后通过LINQ进行操作。

我在表单上有一个ListView,datacontext设置为.DataContext = dr.FindAllBuyOrders(),返回一个IQueryable BuyOrder对象。一切正常。但是,当我通过带有ObjectDataProvider的xaml执行相同操作时:

<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.

1 个答案:

答案 0 :(得分:2)

我找到了解决方案:

connectionstring存储有数据库的相对路径而不是绝对路径。在此链接中查看更多信息:

Embedding SQL Express to use with Linq to SQL and User Instances can be a pain