无法使用数据库第一个模板创建控制器

时间:2016-10-13 19:09:36

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

我正在使用Entity框架数据库第一种方法。我需要对User类的属性进行验证,因此创建了一个包含所有验证的部分类和元数据类。

由Enitity框架创建的类<​​/ p>
namespace OnlineTest
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public User()
        {
            this.tbl_purchase = new HashSet<Purchase>();
        }

        public int UserId { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string email { get; set; }
        public bool EmailVerified { get; set; }
        public string PhoneNo { get; set; }
        public bool PhoneVerified { get; set; }
        public string positionInBank { get; set; }
        public string bankState { get; set; }
        public string bankCity { get; set; }
        public string bankPin { get; set; }
        public string bankAddress { get; set; }
        public string userType { get; set; }
        public bool isActive { get; set; }
        public System.DateTime dateRegistered { get; set; }
        public System.DateTime lastLogin { get; set; }

        public virtual ICollection<Purchase> tbl_purchase { get; set; }
    }
}

部分和元数据类

namespace OnlineTest.Models
{
    [MetadataType(typeof(UserMetadata))]
    public partial class User
    {
        [NotMappedAttribute]
        [Compare("password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class UserMetadata
    {
        [Required(ErrorMessage = "Please type a username")]
        [Display(Name = "UserName")]
        public string username { get; set; }
        [Required(ErrorMessage = "Please type a EmailId")]
        [EmailAddress(ErrorMessage = "E-mail is not valid")]
        [Display(Name = "Email address")]
        public string email { get; set; }
        [Required(ErrorMessage = "Please type a Phone number")]
        [Display(Name = "Phone Number")]
        public string PhoneNo { get; set; }
        [Required(ErrorMessage = "Please provide your position in the bank")]
        [Display(Name = "Position in bank")]
        public string positionInBank { get; set; }
        [Required(ErrorMessage = "Please provide your bank's address")]
        [Display(Name = "Bank's Address")]
        public string bankAddress { get; set; }
        [Required(ErrorMessage = "Please provide state where bank is situated")]
        [Display(Name = "State")]
        public string bankState { get; set; }
        [Required(ErrorMessage = "Please provide city where bank situated")]
        [Display(Name = "City")]
        public string bankCity { get; set; }
        [Required(ErrorMessage = "Please provide pincode of your banks location")]
        [Display(Name = "Pincode")]
        public string bankPin { get; set; }
        [Required(ErrorMessage = "Please type a password")]
        [Display(Name = "Password")]
        public string password { get; set; }
    }
}

但是当我试图生成控制器时,它会抛出以下错误。

enter image description here

任何帮助都会很明显

1 个答案:

答案 0 :(得分:1)

您的部分类定义位于两个不同的名称空间中,这实际上意味着您要定义两个单独的类:OnlineTest.UserOnlineTest.Models.User。这为EF创建了一个命名冲突,因为它试图在同一个模型中创建两个具有相同名称的实体。