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

我用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.
有帮助吗?

解决方案

我找到了解决方案:

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

嵌入SQL Express以与Linq一起使用到SQL和用户实例可能会很痛苦

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top