簡體   English   中英

嘗試連接 SQL 服務器和 ASP.NET MVC mvc 應用程序

[英]Trying connect SQL Server and ASP.NET MVC mvc app

我正在嘗試使用 ASP.NET MVC 創建一個股票跟蹤系統,並使用 SQL 服務器創建了一個數據庫,但我無法從我的應用程序連接它。

當我嘗試開始時。 VS 給我這個錯誤任何人都可以幫助我嗎?

在此處輸入圖像描述

這是我的Program.csDBContext.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));

數據庫上下文.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; }
    }
}

修改您的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM