Dam*_*les 28 c# sqlite asp.net-identity asp.net-core
我使用的是 ASP.NET Core 5.0 + SQL Server 的教程,但实际上我使用的是 ASP.NET Core 6.0 + Sqlite。
该教程中有以下代码StartUp.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnStr")));
}
Run Code Online (Sandbox Code Playgroud)
但在我的项目中,该文件或类不存在。有一个Program.cs文件没有类或方法,只有代码行。我猜想它就是取代该类的东西,所以我尝试使用它
builder.Services.AddDbContext<ApplicationDbContext>(options=> options.);
Run Code Online (Sandbox Code Playgroud)
options没有这样的方法UseSqlServer。我认为这是因为我使用的是 Sqlite,而不是 SQL Server,所以我在网上搜索了 Sqlite 的示例,但这些示例中的方法也不存在。我可以看到AddEntityFrameworkSqlite,但仅此而已。
我怎样才能做到这一点?
我添加了以下相关包:
其他课程与原教程相同。
这是DbContext班级。
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext:IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
{
}
}
Run Code Online (Sandbox Code Playgroud)
Program.cs我试图编辑的代码:
using WebApplication1.Authentication;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<ApplicationDbContext>(options=> options.);
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
小智 36
参考@ussimandias 提供的ASP.NET Core 6.0 Minimal API with Entity Framework core,它也对我有用。
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
DbContext.cs,重写通过文件
OnConfiguring从SQL Server读取数据库连接字符串的方法appsettings.json
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var connectionString = configuration.GetConnectionString("AppDb");
optionsBuilder.UseSqlServer(connectionString);
}
Run Code Online (Sandbox Code Playgroud)
Program.cs,使用服务容器设置依赖注入
var connectionString = builder.Configuration.GetConnectionString("AppDb");
builder.Services.AddDbContext<EmployeeDbContext>(x => x.UseSqlServer(connectionString));
Run Code Online (Sandbox Code Playgroud)
在 Nuget 包管理器控制台(工具 > Nuget 包管理器 > 包管理器控制台)上:
dotnet tool install dotnet-ef -f.dotnet ef database updatedotnet ef database update创建的迁移脚本:
namespace MiniDemo.Migrations
{
[DbContext(typeof(EmployeeDbContext))]
[Migration("20210725025828_initialDb")]
partial class initialDb
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.8")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("MiniDemo.Model.Employee", b =>
{
b.Property<string>("EmployeeId")
.HasColumnType("nvarchar(450)");
b.Property<string>("Citizenship")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.HasKey("EmployeeId");
b.ToTable("Employee");
});
#pragma warning restore 612, 618
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 15
你必须安装这些包
在程序.cs中
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
Run Code Online (Sandbox Code Playgroud)
在 appsettings.json 中
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75313 次 |
| 最近记录: |