0

I am using Entity framework database first approach .I needed to put validation on the properties of my User class, so created a partial class and a metadata class with all the validations.

class created by Enitity framework

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; }
    }
}

Partial and metadata classes

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; }
    }
}

But when I am trying to generate the controller it is throwing the below error.

enter image description here

Any help will be appreciable

Shahzad Ahamad
  • 809
  • 1
  • 11
  • 30
  • Lot's of potential causes. Try some of these: http://stackoverflow.com/questions/19920837/there-was-an-error-running-the-selected-code-generator-in-vs-2013-scaffolding – Steve Greene Oct 13 '16 at 19:15
  • @SteveGreene I don't think my issue is similar to the link you gave me. I tried some methods given in the answers but it is not working – Shahzad Ahamad Oct 13 '16 at 19:26
  • @simba Ideally, you do not want to use same Entity class for Model. Why not create a new Model class with DataAnnotation? – Win Oct 13 '16 at 19:28
  • @SteveGreene Above error is saying both 'OnlineTest.Models.User' and 'OnlineTest.User' have the same simple name of 'User' and so cannot be used in same model. I am unable to undertsand what is the issue. Can u please help me with this? – Shahzad Ahamad Oct 13 '16 at 19:29

1 Answers1

1

Your partial class definitions are in two different namespaces, which essentially means you are defining two separate classes: OnlineTest.User and OnlineTest.Models.User. This is creating a naming conflict for EF as it is trying to create two entities in the same model with the same name.

Oggy
  • 1,516
  • 1
  • 16
  • 22