没有定义键的类

时间:2015-01-05 06:36:15

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

我试图添加一个控制器,它一直给我和错误,因为它说无法检索元数据。在模型生成期间检测到一个或多个验证错误。还有一些关于没有定义键的事情。

这是我的模特

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

namespace User_Management.Models
{
    public class UserDB
    {
        public string Username { get; set; }
        public int EmployeeId { get; set; }
        public string Email { get; set; }
        public string FullName { get; set; }
        public string Password { get; set; }
        public DateTime JoinDate { get; set; }
        public string Position { get; set; }
        public string Team { get; set; }
        public string Security { get; set; }
        public int Status { get; set; }
    }

    public class UserDBContext : DbContext
    {
        public DbSet<UserDB> Users { get; set; }
    }
}

这是我的sql

CREATE TABLE [dbo].[UserDBs] (
    [EmployeeId] INT            IDENTITY (1, 1) NOT NULL,
    [Username]   NCHAR (15)     NULL,
    [Email]      NVARCHAR (MAX) NOT NULL,
    [FullName]   NCHAR (20)     NULL,
    [Password]   NCHAR (15)     NOT NULL,
    [JoinDate]   DATETIME       NOT NULL,
    [Position]   NVARCHAR (MAX) NOT NULL,
    [Team]       NVARCHAR (MAX) NOT NULL,
    [Security]   NVARCHAR (40)  NOT NULL,
    [Status]     INT            NOT NULL,
    CONSTRAINT [PK_UserDBs] PRIMARY KEY CLUSTERED ([EmployeeId] ASC)
);

我仍然在试图为此添加控制器...

1 个答案:

答案 0 :(得分:0)

尝试添加:

[Key]
public int Id(or UserId) in UserDB class