UWP EF核心现有数据库

时间:2019-06-24 19:37:59

标签: c# .net-core uwp-xaml ef-core-2.1

我正在尝试使用UWP Application连接到现有的SQL Server数据库,并且正在使用EF Core 2.2.4,但是我收到以下错误:

System.Data.SqlClient.SqlException:'与SQL Server建立连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:TCP提供程序,错误:40-无法打开与SQL Server的连接)'

我尝试使用google和浏览论坛在互联网上研究问题,但到目前为止,似乎没有任何东西可以向我指出解决方案。我确实找到了一个答案表'16,说明SQLite是3年前EF&UWP支持的唯一数据库选项。

我实质上只是想获取返回的表对象的列表,以在ListView中的UWP上显示。 EF核心位于一个名为“ DataProject”的单独项目中,所有类均位于models文件夹中,该文件夹添加为对我的UWP项目“ UWPProject”的引用。一切都可以编译,但是在调试时,当我尝试使用EF Core从SQL数据库中提取数据时,出现错误40消息。

UWPProject - MainPage.xaml File
<Page
    x:Class="UWPProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LocalPSInvestigation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:models="using:DataProject.Models"
    Loaded="Page_Loaded"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="My Complaints/Requests" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="MS Reference Sans Serif" FontSize="24" Width="300"/>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBox x:Name="idBox" Grid.Column="0" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="ID" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0" Width="320" />
            <TextBox x:Name="dateBox" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Date" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="addrBox" Grid.Column="2" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Address" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="countyBox" Grid.Column="3" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="County" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="cityBox" Grid.Column="4" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="City" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="phoneBox" Grid.Column="5" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Phone" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="searchBox" Grid.Column="4" Grid.Row="1" TextWrapping="Wrap" Text="" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBlock x:Name="searchLabel" Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="Search:" VerticalAlignment="Center" HorizontalAlignment="Right" FontFamily="MS Reference Sans Serif" FontSize="12"/>
            <ListView Name="Complaints" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.IsVerticalRailEnabled="True" ScrollViewer.VerticalScrollMode="Enabled"
                      ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.IsHorizontalRailEnabled="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Grid.Row="2"
                      Grid.Column="0" Grid.ColumnSpan="6" Width="1920" Height="200">
                <ListView.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="ID" TextAlignment="Center" Width="320" />
                            <TextBlock Text="Date" TextAlignment="Center" Width="320" />
                            <TextBlock Text="Business Name" TextAlignment="Center" Width="200" />
                            <TextBlock Text="Address" TextAlignment="Center" Width="320" />
                            <TextBlock Text="County" TextAlignment="Center" Width="320" />
                            <TextBlock Text="City" TextAlignment="Center" Width="240" />
                            <TextBlock Text="Phone" TextAlignment="Center" Width="200" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.HeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="models:TableObject">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="ComplaintId" TextAlignment="Center" Text="{x:Bind ProgramComplaintId}" Width="320"/>
                            <TextBlock Name="Date" TextAlignment="Center" Text="{x:Bind Date}" Width="320"/>
                            <TextBlock Name="BusinessName" TextAlignment="Center" Text="{x:Bind BusinessName}" Width="200"/>
                            <TextBlock Name="Address" TextAlignment="Center" Text="{x:Bind Address}" Width="320"/>
                            <TextBlock Name="County" TextAlignment="Center" Text="{x:Bind County}" Width="320"/>
                            <TextBlock Name="City" TextAlignment="Center" Text="{x:Bind City}" Width="240"/>
                            <TextBlock Name="Phone" TextAlignment="Center" Text="{x:Bind Phone}" Width="200"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </StackPanel>
</Page>
UWPProject - MainPage.xaml.cs File
------------------------------------------------------------------------------
private void Page_Loaded(object sender, RoutedEventArgs e)
{
            using(var db = new dbContext())
            {
                ListViewName.ItemsSource = db.TableObject.ToList();
            }
}

------------------------------------------------------------------------------
DataProject - DBContext.cs File
------------------------------------------------------------------------------
public DbContext()
{
}

public DbContext(DbContextOptions<DbContext> options)
            : base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseSqlServer("Server=XXXXXXX;Database=XXXXX;user id=XXXXXXXXX;password=XXXXXXX");
            }
}
------------------------------------------------------------------------------

我希望这能提取表对象的列表并将其分配给项源,但我收到连接错误。我知道指定的用户名/密码有效,并且服务器和数据库存在,因为我已经使用了scaffold命令来生成类。

UWP是否支持使用EF Core进行SQL Server连接?我似乎找不到能说明这种情况的东西。

1 个答案:

答案 0 :(得分:0)

感谢Xavier Xie的建议,但问题似乎出在实际上我没有启用以下功能,如下图所示。

DatabaseIssue