0

I have been learning asp.net mvc for a while now and I am coming to grips with using the entity framework,

I have the following Model

[Table("User")]
public partial class User
{
    //  [ConcurrencyCheck]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]

    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "User Name")]
    [Remote("IsUserNameAvailable", "User", ErrorMessage = "User name already Exists.")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [StringLength(150, MinimumLength = 6)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string Surname { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    [Remote("IsEmailAvailable", "User", ErrorMessage = "Email Address Already Exists.")]
    public string Email { get; set; }

}

This all works fine when I register a user. But my problem is that I tried Doing something like this for login

[Table("User")]
public partial class Login
{
    //  [ConcurrencyCheck]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]

    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [StringLength(150, MinimumLength = 6)]
    [Display(Name = "Password")]
    public string Password { get; set; }


}

The reason I wanted this was so that I can use a model that would not have to use the remote Attribute as I wouldn't be checking if the username exists

The error I get when I do this says that I cannot use separate entities for the same table. I am trying to get around not triggering the remote attribute for the login part.

Has anyone come across this?

Thanks,

tereško
  • 58,060
  • 25
  • 98
  • 150
Brian
  • 59
  • 2
  • 8
  • You need one data model (which should not include view specific attributes such a `[Remote]`) and then use view models - refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Apr 07 '16 at 21:24

1 Answers1

0

remove access modifier partial and [Table("User")] ,[Key] attributes

rashfmnb
  • 9,959
  • 4
  • 33
  • 44
  • Many thanks for the answer but How does my model know how my Tables are mapped if I remove the Table? And I have An Id as primary key, what would be the reason it is caused by that? – Brian Apr 07 '16 at 18:30
  • actually this will be a model class to show the data on the view when someone submits the data at that time you will convert that data from your model class to entity class so on the model class you don't have to mention that what is you key field and etc – rashfmnb Apr 07 '16 at 18:32