开发者社区> 问答> 正文

即使在模型和数据库中都定义了键,我仍然收到错误“ EntityType没有定义键”

我一直在关注如何在MVC中处理多个表的本教程,并且在尝试为Index(本教程的第12步)指定“ EntityType未定义键”的视图时遇到错误。我能够找到的唯一解决方案是在模型类的主键成员之前放置[Key]属性。但是,当我尝试再次创建视图时,出现了相同的错误。

控制器代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RelationalDatabaseWebsite.Models;

namespace RelationalDatabaseWebsite.Controllers
{
    public class UsersTablesController : Controller
    {
        private ApprenticeTestEntities db = new ApprenticeTestEntities();

        public ActionResult Index()
        {
            UsersLogContext cs = new UsersLogContext();
            List<Log> logs = cs.Log.ToList(); 
            return View(logs);
        }
日志模型的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;



namespace RelationalDatabaseWebsite.Models
{
    [Table("LogTable")]
    public class Log
    {
        [Key]
        public int LogTableID { get; set; }

        public int UserID { get; set; }
        public string TimeLogged { get; set; }

        public virtual UsersTable Users { get; set; }
    }
}

用户模型代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;


namespace RelationalDatabaseWebsite.Models
{
    [Table("UsersTable")]
    public class Users
    {
        [Key]
        public int UserID { get; set; }

        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Password { get; set; }
        public string Salt { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }
        public bool IsAdmin { get; set; }

        public virtual ICollection<AddressTable> Address { get; set; }
        public virtual ICollection<DOBTable> DOB { get; set; }
        public virtual ICollection<LogTable> Log { get; set; }


    }
}
DbContext模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace RelationalDatabaseWebsite.Models
{
    public class UsersLogContext : DbContext 
    {
        public DbSet<Users> Users { get; set; }
        public DbSet<Log> Log { get; set; }

    }
}

用户数据库表

CREATE TABLE [dbo].[UsersTable](
    [UserID] [uniqueidentifier] NOT NULL,
    [FirstName] [varchar](50) NOT NULL,
    [Surname] [varchar](50) NOT NULL,
    [Password] [varchar](50) NOT NULL,
    [Salt] [varchar](50) NOT NULL,
    [PhoneNumber] [varchar](50) NULL,
    [Email] [nvarchar](50) NOT NULL,
    [IsAdmin] [bit] NOT NULL,
 CONSTRAINT [PK_UsersTable] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
日志数据库表

CREATE TABLE [dbo].[LogTable](
    [LogTableID] [int] IDENTITY(1,1) NOT NULL,
    [UserID] [uniqueidentifier] NOT NULL,
    [TimeLogged] [datetime] NOT NULL,
 CONSTRAINT [PK_LogTable_1] PRIMARY KEY CLUSTERED 
(
    [LogTableID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[LogTable]  WITH CHECK ADD  CONSTRAINT [FK_LogTable_UsersTable] FOREIGN KEY([UserID])
REFERENCES [dbo].[UsersTable] ([UserID])
GO

ALTER TABLE [dbo].[LogTable] CHECK CONSTRAINT [FK_LogTable_UsersTable]
GO

对大量的代码表示歉意,只是考虑到Log和Users表都引发了错误,我会把这两部分都放下来,以防各个错误彼此相关。

展开
收起
心有灵_夕 2019-12-07 23:08:31 772 0
1 条回答
写回答
取消 提交回答
  • 您的Users表的键类型为uniqueidentifier,但是代码模型的键类型为int

    [Key]
    public int UserID { get; set; }
    ...
    
    [UserID] [uniqueidentifier] NOT NULL,
    
    

    尝试将用户模型中的键(及其在Log模型中的相关属性)更改为Guid...

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserID { get; set; }
    
    2019-12-07 23:09:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载