简体   繁体   English

无法使用 Blazor 和实体框架连接到 SQL 数据库

[英]Cannot connect to SQL database using Blazor and Entity Framework

I'm trying to setup a simple Blazor website that connects to a SQL Server database and I can't seem to get it to work, I get an error:我正在尝试设置一个简单的 Blazor 网站,该网站连接到 SQL 服务器数据库,但我似乎无法让它工作,我收到一个错误:

System.PlatformNotSupportedException: Strings.PlatformNotSupported_DataSqlClient System.PlatformNotSupportedException:Strings.PlatformNotSupported_DataSqlClient

when trying to query any of the database entities.尝试查询任何数据库实体时。 Connecting to the same database and using the same DBContext works fine from a unit testing projects, I just can't make it work using Blazor.连接到同一个数据库并使用相同的DBContext在单元测试项目中工作正常,我只是无法使用 Blazor 使其工作。

In my program.cs I have:在我的 program.cs 中,我有:

builder.Services.AddDbContext<IntegrationDBContext>(options => options.UseSqlServer(@"MyConnString"));

I even tried using an in-memory provider, which worked fine.我什至尝试使用内存提供程序,效果很好。 I'm using .NET 6 and connecting to an Azure based SQL (although I did try connecting to a LocalDB instance, with the same results).我正在使用 .NET 6 并连接到基于 Azure 的 SQL (尽管我确实尝试连接到 LocalDB 实例,结果相同)。

Thanks!谢谢!

The MS-SQL Client software uses a TCP socket based protocol that is indeed "not supported" on WebAssembly. MS-SQL 客户端软件使用基于 TCP 套接字的协议,该协议在 WebAssembly 上确实“不支持”。

You would need a Db with an HTTP based API but then be very aware that your credentials will be public.您将需要一个带有基于 HTTP 的 API 的数据库,但请注意您的凭据将是公开的。 A Web based Client is not secure.基于 Web 的客户端不安全。

This is why all SPA apps that need a backend Database use an API server for Db access.这就是为什么所有需要后端数据库的 SPA 应用程序都使用 API 服务器进行 Db 访问。 Take a look at the Blazor Wasm + Hosted template, see if that works for you.查看 Blazor Wasm + Hosted 模板,看看它是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首先使用Entity Framework代码时无法连接到数据库 - Cannot connect to database while using Entity Framework Code First 无法在Oracle数据库的实体框架中生成SQL - Cannot generate sql in entity framework for oracle database 使用实体框架连接到Oracle数据库 - Connect to Oracle database using Entity Framework 使用 Entity Framework 6 将父实体与数据库中现有的子实体连接起来 - Connect parent entity with existing child entity in database using Entity Framework 6 如何使用 Entity Framework Core 和 Database First 在 MacOS 上连接到 SQL 数据库 - How to connect to a SQL database on MacOS with Entity Framework Core and Database First 如何使用已在Azure中注册的应用程序使用实体框架数据库第一种方法连接到Azure SQL DB - How to connect to Azure SQL DB using entity framework database first approach using already registered app in Azure 使用没有实体框架的 .NET 核心中的访问令牌连接到 Azure SQL 数据库 - Connect to Azure SQL Database using access token in .NET Core without Entity Framework WCF RIA服务+实体框架4无法连接到数据库 - WCF RIA Services + Entity Framework 4 cannot connect to database 实体框架 (EF) 和 .Net 6 Blazor 不更新数据库 - Entity Framework (EF) and .Net 6 Blazor not updating database 在SQL CE数据库上的Entity Framework中使用DateDiff - Using DateDiff in Entity Framework on a SQL CE database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM