简体   繁体   English

尝试连接 SQL 服务器和 ASP.NET MVC mvc 应用程序

[英]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.csDBContext.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.

相关问题 从ASP.NET MVC应用程序连接到SQL Server - Connect to SQL Server from ASP.NET MVC application ASP.NET MVC 3应用程序无法连接到AWS实例上的SQL Server 2008实例 - ASP.NET MVC 3 app cannot connect to SQL server 2008 instance on AWS instance ASP.NET MVC Web应用程序用户登录后,SQL Server Express无法连接而没有任何有用的错误信息 - SQL Server Express fails to connect without any useful error information after ASP.NET MVC Web App user login 如何将 ASP.NET Core 5 MVC 项目连接到 Supabase 数据库而不是 SQL 服务器? - How to connect ASP.NET Core 5 MVC project to Supabase database instead of SQL Server? ASP.NET MVC 4 EF6 无法连接到 SQL Server Express 数据库 - ASP.NET MVC 4 EF6 cannot connect to SQL Server Express database ASP.NET MVC 无法连接到我的本地 SQL Server 数据库 - ASP.NET MVC cannot connect to my local SQL Server database ASP.NET 5 MVC:无法连接到 Web 服务器“IIS Express” - ASP.NET 5 MVC: unable to connect to web server 'IIS Express' 无法从Asp.Net MVC连接到远程服务器 - Unable to connect to the remote server from Asp.Net MVC 尝试对角色进行身份验证或访问时,ASP.NET MVC SQL Server是否不可访问? - ASP.NET MVC SQL Server not accesable when trying to authenticate or access roles? 尝试使用C#从ASP.net MVC 4查询SQL Server - Trying to Query a SQL Server from ASP.net MVC 4 using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM