我嘗試使用創建新資料庫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;}
}
}
鏈接.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;}
}
程式.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();
uj5u.com熱心網友回復:
1.在 Program.cs 中添加以下代碼:
builder.services.AddDbContext<Sooziales_NetzwerkDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection1"]));
在 appsettings.json 檔案中添加了一個連接字串
"ConnectionStrings":如下所示:"DefaultConnection1": "Server=(localdb)\\mssqllocaldb;Database=Sooziales_NetzwerkDbContext -7dc5b790-765f-4381-988c-5167405bb107;Trusted_Connection=True;MultipleActiveResultSets=true"
uj5u.com熱心網友回復:
將此添加到 Program.cs:
builder.Services.AddDbContext<Sooziales_NetzwerkDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("LinkConnection")));
這對于 appsettings.json:
"LinkConnection": "DataSource=links.db"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450080.html
