创建EfCore时踩过的坑

1、系统报错:An error occurred while saving the entity changes. See the inner exception for details。

原因: 表中keyId为主键且设置为自增,不需要在此处加入值,去掉KeyId=8 就可以了。如果不设置为自增,则需要加才行。

2、A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 证书链是由不受信任的颁发机构颁发的。)”

 原因:连接字符串的问题。原来是:

optionsBuilder.UseSqlServer("Data Source=DESKTOP-LHCC2G9\\SQLEXPRESS;Initial Catalog=InfoData;Integrated Security=True;");

改为:optionsBuilder.UseSqlServer("Server=DESKTOP-LHCC2G9\\SQLEXPRESS;Database=InfoData;Encrypt=True;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true;");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 EF Core 创建 SQLite 数据库,可以按照以下步骤进行: 1. 安装 EF Core 和 SQLite NuGet 包。 在 Visual Studio 中,可以通过 NuGet 包管理器来安装 EF Core 和 SQLite。可以在“工具”->“NuGet 包管理器”->“程序包管理器控制台”中使用以下命令来安装它们: ```bash Install-Package Microsoft.EntityFrameworkCore.Sqlite Install-Package Microsoft.EntityFrameworkCore.Tools ``` 2. 创建 DbContext 类。 创建一个类继承自 DbContext,该类表示一个数据库上下文,用于访问和操作数据库。在 DbContext 类中,可以通过 DbSet 属性来表示要在数据库中创建的实体类。 ```csharp public class MyDbContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite("Data Source=mydatabase.db"); } ``` 上述代码表示创建了一个 DbContext,其中包含一个名为 People 的 DbSet 属性,表示要在数据库中创建一个名为 "People" 的表。 3. 创建实体类。 在 DbContext 类中,使用 DbSet 属性表示要在数据库中创建的实体类,如上述代码所示。实体类通常包含表中的列,每个属性表示一个列。 ```csharp public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } ``` 上述代码表示创建了一个名为 Person 的实体类,其中包含 Id、Name 和 Age 三个属性,分别表示表中的三个列。 4. 创建数据库。 可以使用以下命令在 SQLite 数据库中创建表: ```bash dotnet ef migrations add InitialCreate dotnet ef database update ``` 执行上述命令后,EF Core 将会创建一个名为 "mydatabase.db" 的 SQLite 数据库,并且在该数据库中创建一个名为 "People" 的表,用于存储 Person 实体类的数据。 希望这些步骤对您有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值