[英]Trying connect SQL Server and ASP.NET MVC mvc app
I'm trying to create a Stock Tracking System with ASP.NET MVC and I created a database with SQL Server but I can't connect this from my app.我正在尝试使用 ASP.NET MVC 创建一个股票跟踪系统,并使用 SQL 服务器创建了一个数据库,但我无法从我的应用程序连接它。
When I try to start.当我尝试开始时。 VS is giving me this error can anyone help me?
VS 给我这个错误任何人都可以帮助我吗?
Here are my Program.cs
and DBContext.cs
files这是我的
Program.cs
和DBContext.cs
文件
Program.cs:程序.cs:
using Microsoft.EntityFrameworkCore;
using stockTrackingSystem.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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();
//DB CONNECTION
var connectionString = builder.Configuration.GetConnectionString("AppDb");
builder.Services.AddDbContext<DBContext>(x => x.UseSqlServer(connectionString));
DBContext.cs:数据库上下文.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.EntityFrameworkCore;
using stockTrackingSystem.Models;
namespace stockTrackingSystem.Data
{
public class DBContext: DbContext
{
public DbSet<Store> Store { get; set; }
public DbSet<Category> Category { get; set; }
public DbSet<Costumer> Costumer { get; set; }
}
}
Modify your DBContext class to include a constractor.修改您的 DBContext class 以包含一个构造函数。
public class DBContext: DbContext
{
public DBContext(DbContextOptions<DBContext> options)
: base(options)
{
}
public DbSet<Store> Store { get; set; }
public DbSet<Category> Category { get; set; }
public DbSet<Costumer> Costumer { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.