[英]Why is my Dbconext SQL connection not working
我是 ASP.NET Core 的新手,一直在使用 Entity Framework Core 来处理我本地机器上的 SQL 服务器数据库。 我最近的项目使用 .NET 7 运行时而不是 6 和 Web App Core 模板而不是 MVC 模板。
到目前为止,这是我的项目的布局。
程序.cs
using CommentsApplication.Db;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<DBconnection>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("localhost"));
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Dbconnection.cs
这是连接到我的数据库的DbContext
。 我在数据库中有一个名为accounts
的表。
using CommentsApplication.Models;
using Microsoft.EntityFrameworkCore;
namespace CommentsApplication.Db
{
public class DBconnection: DbContext
{
public DBconnection(DbContextOptions<DBconnection> options) : base(options) { }
public DbSet<Account> accounts { get; set; }
}
}
这是Account
class 的样子
using System.ComponentModel.DataAnnotations;
namespace CommentsApplication.Models
{
public class Account
{
[Key]
public int Id { get; set; }
public string Organization { get; set; }
public string Email { get; set; }
}
}
连接字符串如下所示(我的计算机名称是 ZURG)
Server=ZURG;Database=AccountsDB;Trusted_Connection=True;Encrypt=False;
当我尝试运行该应用程序时出现此错误:
InvalidOperationException:尝试激活“CommentsApplication.Pages.IndexModel”时无法解析“System.Data.Common.DbConnection”类型的服务。
这就是IndexModel
的样子:
using CommentsApplication.Models;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.Common;
namespace CommentsApplication.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly DbConnection _db;
public string org { get; set; }
public string email { get; set; }
public IndexModel(ILogger<IndexModel> logger, DbConnection DB)
{
_logger = logger;
_db = DB;
}
public void OnGet()
{
}
public void OnPost(string org, string email)
{
org = org.Trim();
email = email.Trim();
Account a = new Account()
{
Organization= org,
Email = email
};
}
}
}
每次我在我的其他 ASP.NET Core 项目中使用类似的设置时,它都运行良好,但由于某种原因这次不会。
有人能指出我正确的方向吗?
你搞砸了你的命名......你称你的 DbContext“Dbconnection”注意小写“c”,但添加了一个类型“DbConnection”的引用所以你的视图模型正在寻找 System.Data.Common.DbConnection class。
我建议将您的 DbContext 命名为“AppDbContext”之类的名称,以避免命名混淆,这将使像这样的案例问题更容易发现/避免。
在您的情况下,您引用了命名空间: System.Data.Common
,还有一个名为 class 的DBconnection
,而您没有引用命名空间: CommentsApplication.Db
,所以如果您不写 DBconnection class 的全名,默认为 System.Data.Common.DBconnection
如果您的代码中有类在不同的命名空间下共享相同的名称,请尝试使用全名以避免混淆,
在你的情况下,它应该是CommentsApplication.Db.DBconnection
private readonly CommentsApplication.Db.DbConnection _db
public IndexModel(ILogger<IndexModel> logger, CommentsApplication.Db.DbConnection DB)
{
_logger = logger;
_db = DB;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.