I've been following this tutorial,当尝试为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表都引发了错误,如果各个错误彼此相关,我会把这两部分都放下来。
答案 0 :(得分:3)
您的Users
表的键类型为 uniqueidentifier ,但是您的代码模型的键类型为 int
[Key]
public int UserID { get; set; }
...
[UserID] [uniqueidentifier] NOT NULL,
尝试将用户模型中的密钥(及其在Log模型中的相关属性)更改为Guid
...
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserID { get; set; }