【问题标题】:SqlException: Invalid object name 'dbo.ApplicationUsers'SqlException:无效的对象名称“dbo.ApplicationUsers”
【发布时间】:2018-02-04 15:23:10
【问题描述】:

从我的Customer 视图显示AspNetUser.Email 时,我收到此错误。我在CustomerController 上执行.Include(AspNetUser) 并引用数据库表名AspNetUser,其中Email 来自。不确定我在这里缺少什么,但我需要能够从 Customer 视图中调用 AspNetUser.Email

MyApplicationUser 来自我的IdentityModel,我已将其包含在下面。

在我的Customer 表中,我有一个Customer.UserId 列,它是AspNetUser.Id 列的外键。

客户模型

namespace DemoPool.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Customer")]
    public partial class Customer
    {
        [StringLength(128)]
        public string UserId { get; set; }

        [ForeignKey("UserId")]
        public virtual ApplicationUser AspNetUser { get; set; }
    }
}

客户控制器

// GET: Customers
        [Authorize(Roles = "Admin, User")]
        public ActionResult Index()
        {
            var customers = db.Customers.Include(c => c.AspNetUser);
            return View(customers.ToList());

        }

客户视图

@model IEnumerable<DemoPool.Models.Customer>
<table class="table">
  <tr>
    <td>               
      @Html.DisplayFor(modelItem => item.AspNetUser.Email)
    </td>
  </tr>
</table>

身份模型

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

namespace DemoPool.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    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 ApplicationDbContext()
            : base("DemoPoolEntities", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

【问题讨论】:

  • 您可以为ApplicationUser发布您的实体,这是一个sql错误,该表不存在
  • 是的,我刚刚添加了它。 ApplicationUser 在我的数据库中不存在。那么我应该将 Application User : IdentityUser 更改为 AspNetUser : IdentityUser 吗? @VidmantasBlazevicius
  • 通过在 Customer 类中的虚拟属性上指定 [ForeignKey] 属性,您基本上是在告诉 EF 您有一个数据库表,并且关系将得到解决。如果我正确理解了这个问题,那么您需要 AspNetUser 实体类才能使其工作。
  • 做到了。谢谢。
  • 不客气,我把它作为答案发布了。

标签: c# sql-server asp.net-mvc entity-framework


【解决方案1】:

通过在Customer 类中的虚拟属性上指定[ForeignKey] 属性,您基本上是在告诉EF 您有一个数据库表,并且关系将得到解决。如果我正确理解了这个问题,那么您需要AspNetUser 实体类才能使其工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多