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

4
  • Lot's of potential causes. Try some of these: stackoverflow.com/questions/19920837/… Commented Oct 13, 2016 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 Commented Oct 13, 2016 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? Commented Oct 13, 2016 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? Commented Oct 13, 2016 at 19:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So should i make changes in the class generated by the EF because as per my knowledge it is not a good practice to make changes in that class
Why not just change the namespace of your new class to 'OnlineTest' instead of 'OnlineTest.Models'?
Just did that but it is picking all the fields and confirm password field is not showing.I.e it is picking the class generated by the EF
It resolved my original issue thank you for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.