【问题标题】:I'm getting the error 'EntityType has no key defined' even though I've defined a key in both the model and the database我收到错误“EntityType has no key defined”,即使我在模型和数据库中都定义了一个键
【发布时间】:2020-03-31 08:58:28
【问题描述】:

I've been following this tutorial 关于如何在 MVC 中处理多个表,我在尝试为 Index 创建视图(本教程中的第 12 步)指定“EntityType has no key defined”时遇到了一个错误。我能找到的唯一解决方案是在模型类的主键成员之前放置一个 [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);
        }

Log 模型代码

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 表都引发了错误,我会将这两部分都放下,以防各自的错误相互关联。

【问题讨论】:

    标签: c# .net sql-server entity-framework model-view-controller


    【解决方案1】:

    您的 Users 表的键类型为 uniqueidentifier,但您的代码模型的键类型为 int

    [Key]
    public int UserID { get; set; }
    

    ...

    [UserID] [uniqueidentifier] NOT NULL,
    

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

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserID { get; set; }
    

    【讨论】:

    • 因为 uniqueidentifier 列也是您的关键列,所以数据库必须生成 UserID。您需要将此告知 EF:例如[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    • 感谢@DanielSchmid,我已经编辑了答案以包含您的更正。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2021-05-24
    • 2021-05-26
    相关资源
    最近更新 更多