我一直在关注如何在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表都引发了错误,我会把这两部分都放下来,以防各个错误彼此相关。
您的Users表的键类型为uniqueidentifier,但是代码模型的键类型为int
[Key]
public int UserID { get; set; }
...
[UserID] [uniqueidentifier] NOT NULL,
尝试将用户模型中的键(及其在Log模型中的相关属性)更改为Guid...
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserID { get; set; }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。