simba simba - 1 month ago 16
C# Question

Unable to create controller using database first template

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

Answer

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.