C# ASP.NET MVC 5:我的应用程序没有';我不能从我的数据库中读取数据

C# ASP.NET MVC 5:我的应用程序没有';我不能从我的数据库中读取数据,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我试图学习ASP.NET MVC 5教程,但当我试图在页面上打印“Customers”表中的所有数据时,它不会显示任何内容,即使我的表不是空的。我为此使用变量“ApplicationDbContext\u context”,并将其传递给视图,在视图中打印内容。我错过了什么 我的客户控制员: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mv

我试图学习ASP.NET MVC 5教程,但当我试图在页面上打印“Customers”表中的所有数据时,它不会显示任何内容,即使我的表不是空的。我为此使用变量“ApplicationDbContext\u context”,并将其传递给视图,在视图中打印内容。我错过了什么

我的客户控制员:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.ViewModels;
using Vidly.Models;
using System.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;

namespace Vidly.Controllers
{
    public class CustomersController : Controller
    {
        private ApplicationDbContext _context;

        public CustomersController()
        {
            _context = new ApplicationDbContext();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }
        // GET: Customers
        public ActionResult Index()
        {
            var customers = _context.Customers.ToList();

            var movies = new List<Movie>
            {
                new Movie {Id=1, Name="Movie1"},
                new Movie {Id=2, Name="Movie2"}
            };

            var viewModel = new RandomMovieViewModel { Movies = movies, Customers = customers };

            return View(viewModel);
            
        }
        public ActionResult Details(int id)
        {
            var customers = _context.Customers.ToList();
            if (customers == null)
            {
                return HttpNotFound();
            }
            ViewData["CustId"] = id;
            var viewModel = new RandomMovieViewModel { Customers = customers };
            return View(viewModel);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用Vidly.ViewModels;
使用Vidly.模型;
使用System.Data.SqlClient;
使用Microsoft.Extensions.DependencyInjection;
名称空间Vidly.Controllers
{
公共类CustomerController:控制器
{
私有应用程序的bContext\u上下文;
公共CustomerController()
{
_context=新的ApplicationDbContext();
}
受保护的覆盖无效处置(布尔处置)
{
_context.Dispose();
}
//获取:客户
公共行动结果索引()
{
var customers=_context.customers.ToList();
var movies=新列表
{
新电影{Id=1,Name=“Movie1”},
新电影{Id=2,Name=“Movie2”}
};
var viewModel=new randomoviewmodel{Movies=Movies,Customers=Customers};
返回视图(viewModel);
}
公共行动结果详细信息(int id)
{
var customers=_context.customers.ToList();
如果(客户==null)
{
返回HttpNotFound();
}
ViewData[“CustId”]=id;
var viewModel=new randomMoviewmodel{Customers=Customers};
返回视图(viewModel);
}
}
}
启用迁移后,我的IdentityModels.cs文件丢失,这与我遵循的教程不同,因此我自己创建了它:

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System;

namespace Vidly.Models
{
    public class ApplicationUser : IdentityUser
    {

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    //public class ApplicationDbContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {

        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
使用System.Data.Entity;
使用制度;
名称空间Vidly.Models
{
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器
顾客
@foreach(Model.Customers中的var cust)
{
}

编辑:我解决了问题!原来我在创建ASP.NET MVC文件时启用了“无身份验证”。我重新创建了它,将身份验证切换为“个人用户帐户”非常感谢那些试图帮助我的人。

当您将identity services添加到项目中时,应在[Base Folder]下添加一些数据库迁移文件/Data/Migrations/您希望对数据库运行这些操作以更新表。更新包时,任何数据库更改都将放入这些迁移中

在应用程序配置文件(appsettings.json)中,应该有一个连接字符串来创建应用程序数据库上下文

  "ConnectionStrings": {
    "DefaultConnection": "Server=servername;Database=databaseName;User Id=username;Password=password;",
  }
将新条目添加到配置文件中

"InitialiseIdentityDatabase": "true"
我们将使用它来告诉您服务器在启动时初始化数据库。在我们创建表之后,您可以将其设置为false

在startup.cs中创建新函数添加新函数

    /// <summary>
    /// Setup the identity database
    /// </summary>
    /// <param name="app"></param>
    private void InitializeDatabase(IApplicationBuilder app)
    {
        using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
        {
            serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
            // If there are no user identities, create some default ones here.
        }
    }
//
///设置身份数据库
/// 
/// 
私有void初始化数据库(IApplicationBuilder应用程序)
{
使用(var serviceScope=app.ApplicationServices.GetService().CreateScope())
{
serviceScope.ServiceProvider.GetRequiredService().Database.Migrate();
//如果没有用户标识,请在此处创建一些默认标识。
}
}
在“public void Configure(IApplicationBuilder应用程序,IWebHostEnvironment env)”中添加以下行

if (Configuration.GetValue<string>("InitialiseIdentityDatabase") != null && Configuration.GetValue<string>("InitialiseIdentityDatabase").ToUpper() == "TRUE")
                InitializeDatabase(app);
if(Configuration.GetValue(“InitialiseIdentityDatabase”)!=null和&Configuration.GetValue(“InitialiseIdentityDatabase”).ToUpper()=“TRUE”)
初始化数据库(app);

当配置值设置为true时,服务将运行迁移脚本以在数据库中创建标识表。如果更新标识包,请再次将其设置为true并应用任何迁移更新。

添加您的启动文件…如果您指的是名为“startup.cs”的文件,然后我找不到它。我可能根本就没有它。你对此有什么建议吗?没有,你是对的startup.cs是我想要的…我想看看你是如何为上下文注册DI的,这将在startup.cs文件中完成。根据我的研究,我应该在我的属性文件夹中有我的appsettings.json文件。它不在那里。我只有一个名为“AssemblyInfo.cs”的文件。我应该创建appsettings.json文件吗?我也不认为我有startup.cs文件。
if (Configuration.GetValue<string>("InitialiseIdentityDatabase") != null && Configuration.GetValue<string>("InitialiseIdentityDatabase").ToUpper() == "TRUE")
                InitializeDatabase(app);