如何解决ASP.NET MVC 5:我的应用程序未从数据库中读取数据
我试图遵循有关ASP.NET MVC 5的教程,但是当我尝试在页面上打印“客户”表中的所有数据时,即使我的表不为空,它也不会显示任何内容。为此,我使用变量“ ApplicationDbContext _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);
}
}
}
启用迁移后,缺少我的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();
}
}
}
我还缺少本教程中自动构建的一些表,例如AspNetRoles和AspNetUserRoles。我不知道为什么以及如何解决这个问题。
我的客户查看索引:
@model Vidly.viewmodels.RandomMovieviewmodel
@{
ViewBag.Title = "Index";
}
<table class="table table-hover table-bordered">
<tr>
<td scope="row">
<a href="/Home/Index">
Vidly
</a>
</td>
<td scope="row">
<a href="/Customers">
Customers
</a>
</td>
<td scope="row">
<a href="/Movies">
Movies
</a>
</td>
</tr>
</table>
<table class="table table-hover table-bordered">
<tr>
<th>
Customer
</th>
</tr>
@foreach (var cust in Model.Customers)
{
<tr>
<td>
<a href="/Customers/Details/cust.Id">
cust.Name
</a>
</td>
</tr>
}
</table>
编辑:我解决了这个问题!原来,当我创建ASP.NET MVC文件时,我启用了“不进行身份验证”。我重新创建了它,将身份验证切换为“个人用户帐户”,并且它可以正常工作。非常感谢那些试图帮助我的人。
解决方法
向项目添加身份服务时,应在[基本文件夹] / Data / Migrations /下添加一些数据库迁移文件,您希望对数据库运行这些文件以更新表。更新软件包时,所有数据库更改都将放入这些迁移中。
在您的应用程序配置文件(appsettings.json)中,您应该具有一个连接字符串来创建应用程序数据库上下文
"ConnectionStrings": {
"DefaultConnection": "Server=servername;Database=databaseName;User Id=username;Password=password;",}
在配置文件中添加新条目
"InitialiseIdentityDatabase": "true"
我们将使用它来告诉您服务器在启动时初始化数据库。创建表后,可以将其设置为false。
在启动时创建新功能。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.
}
}
在“公共无效配置(IApplicationBuilder应用程序,IWebHostEnvironment env)”中添加以下行
if (Configuration.GetValue<string>("InitialiseIdentityDatabase") != null && Configuration.GetValue<string>("InitialiseIdentityDatabase").ToUpper() == "TRUE")
InitializeDatabase(app);
当配置值设置为true时,该服务将运行迁移脚本以在数据库中创建身份表。如果您更新身份包,请将其再次设置为true并应用所有迁移更新。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。