无法创建 'Sooziales_NetzwerkDbContext' 类型的对象
Unable to create an object of type 'Sooziales_NetzwerkDbContext'
我尝试使用 dotnet ef migrations add InitialCreate --context Sooziales_NetzwerkDbContext
创建一个新数据库,但出现此错误:无法创建 'Sooziales_NetzwerkDbContext' 类型的对象。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728。构建成功,但失败并显示该错误消息。我的数据库使用 Sqlite。这是我的代码:
Sooziales_NetzwerkDbContext:
using Microsoft.EntityFrameworkCore;
using Sooziales_Netzwerk.Models;
namespace Sooziales_Netzwerk.Data{
public class Sooziales_NetzwerkDbContext : DbContext
{
public Sooziales_NetzwerkDbContext(DbContextOptions<Sooziales_NetzwerkDbContext> options)
: base(options)
{
}
public DbSet<Link> Link {get; set;}
}
}
Link.cs:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Sooziales_Netzwerk.Models;
public class Link{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int idLink {get; set;}
[Display(Name = "Enter Link")]
[Required(ErrorMessage = "Upload without a link make no sence")]
public string link {get; set;}
[Display(Name = "Enter Username")]
[Required(ErrorMessage = "Upload without a link make no sence")]
public string username {get; set;}
[Display(Name = "Enter Likes")]
[Required(ErrorMessage = "Upload without a link make no sence")]
public int likes {get; set;}
}
Program.cs:
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Sooziales_Netzwerk.Data;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
1.add 下面代码 Program.cs 中的代码:
builder.services.AddDbContext<Sooziales_NetzwerkDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection1"]));
向 "ConnectionStrings"
中的 appsettings.json 文件添加了连接字符串:如下所示:
"DefaultConnection1": "Server=(localdb)\mssqllocaldb;Database=Sooziales_NetzwerkDbContext -7dc5b790-765f-4381-988c-5167405bb107;Trusted_Connection=True;MultipleActiveResultSets=true"
将此添加到 Program.cs:
builder.Services.AddDbContext<Sooziales_NetzwerkDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("LinkConnection")));
这对 appsettings.json:
"LinkConnection": "DataSource=links.db"
我尝试使用 dotnet ef migrations add InitialCreate --context Sooziales_NetzwerkDbContext
创建一个新数据库,但出现此错误:无法创建 'Sooziales_NetzwerkDbContext' 类型的对象。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728。构建成功,但失败并显示该错误消息。我的数据库使用 Sqlite。这是我的代码:
Sooziales_NetzwerkDbContext:
using Microsoft.EntityFrameworkCore;
using Sooziales_Netzwerk.Models;
namespace Sooziales_Netzwerk.Data{
public class Sooziales_NetzwerkDbContext : DbContext
{
public Sooziales_NetzwerkDbContext(DbContextOptions<Sooziales_NetzwerkDbContext> options)
: base(options)
{
}
public DbSet<Link> Link {get; set;}
}
}
Link.cs:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Sooziales_Netzwerk.Models;
public class Link{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int idLink {get; set;}
[Display(Name = "Enter Link")]
[Required(ErrorMessage = "Upload without a link make no sence")]
public string link {get; set;}
[Display(Name = "Enter Username")]
[Required(ErrorMessage = "Upload without a link make no sence")]
public string username {get; set;}
[Display(Name = "Enter Likes")]
[Required(ErrorMessage = "Upload without a link make no sence")]
public int likes {get; set;}
}
Program.cs:
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Sooziales_Netzwerk.Data;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
1.add 下面代码 Program.cs 中的代码:
builder.services.AddDbContext<Sooziales_NetzwerkDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection1"]));
向
"ConnectionStrings"
中的 appsettings.json 文件添加了连接字符串:如下所示:"DefaultConnection1": "Server=(localdb)\mssqllocaldb;Database=Sooziales_NetzwerkDbContext -7dc5b790-765f-4381-988c-5167405bb107;Trusted_Connection=True;MultipleActiveResultSets=true"
将此添加到 Program.cs:
builder.Services.AddDbContext<Sooziales_NetzwerkDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("LinkConnection")));
这对 appsettings.json:
"LinkConnection": "DataSource=links.db"