UWP EF Core 现有数据库
UWP EF Core Existing Database
我正在尝试使用 UWP 应用程序连接到现有 SQL 服务器数据库并使用 EF Core 2.2.4,但我收到以下错误:
System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)'
我曾尝试使用 google 和浏览论坛在 Internet 上研究该问题,但到目前为止似乎没有任何解决方案。我确实发现了一个关于 SQLite 是 EF 和 UWP 支持的唯一数据库选项的答案表 '16,但那是 3 年前的事了。
我基本上只是想获取返回的 table 对象的列表,以便在我的 UWP 的 ListView 中显示。 EF Core 位于名为 "DataProject" 的单独项目中,模型文件夹中的所有 类 作为对我的 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");
}
}
------------------------------------------------------------------------------
我希望这会提取 table 对象的列表并将其分配给项目源,但我却收到连接错误。我知道指定的 username/password 有效并且服务器和数据库存在,因为我使用脚手架命令生成 类.
UWP 是否支持 SQL 服务器与 EF Core 的连接?我似乎找不到任何说明情况如此的内容。
感谢 Xavier Xie 的建议,但似乎问题实际上是我没有启用以下功能,如下图所示。
我正在尝试使用 UWP 应用程序连接到现有 SQL 服务器数据库并使用 EF Core 2.2.4,但我收到以下错误:
System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)'
我曾尝试使用 google 和浏览论坛在 Internet 上研究该问题,但到目前为止似乎没有任何解决方案。我确实发现了一个关于 SQLite 是 EF 和 UWP 支持的唯一数据库选项的答案表 '16,但那是 3 年前的事了。
我基本上只是想获取返回的 table 对象的列表,以便在我的 UWP 的 ListView 中显示。 EF Core 位于名为 "DataProject" 的单独项目中,模型文件夹中的所有 类 作为对我的 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");
}
}
------------------------------------------------------------------------------
我希望这会提取 table 对象的列表并将其分配给项目源,但我却收到连接错误。我知道指定的 username/password 有效并且服务器和数据库存在,因为我使用脚手架命令生成 类.
UWP 是否支持 SQL 服务器与 EF Core 的连接?我似乎找不到任何说明情况如此的内容。
感谢 Xavier Xie 的建议,但似乎问题实际上是我没有启用以下功能,如下图所示。