Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
can we create 2 classes in mvc models for single table in sql, my requirement is i have validations in one model class, now for further class i dont need any validations i just want to insert the data whichever the data given by user, any suggestion please let me know
Posted
Updated 10-Aug-16 19:52pm
v5
Comments
Kornfeld Eliyahu Peter 6-Dec-15 5:44am    
Yes. You can...
Member 12078709 6-Dec-15 6:42am    
but i am not able to do it.. i am getting exception like
The type ****.Models.***** already contains a definition
Suvendu Shekhar Giri 6-Dec-15 9:09am    
Please update your question with these code blocks and do the necessary formatting.
Member 12078709 6-Dec-15 23:19pm    
I am getting error,
I have one table np_user_dtls with 4 fields in sql server

user_name
user_last_name
arabic_name
nationality
In mvc i created a model class as below

public partial class np_user_dtls
{


[Display(Name = "First Name*")]
[Required(ErrorMessage = "First name is required.")]
public string user_name { get; set; }


[Required(ErrorMessage = "last name is required.")]
public string user_last_name { get; set; }

[Required(ErrorMessage = "arabic name is required.")]
public string arabic_name { get; set; }

[Required(ErrorMessage = "nationality is required.")]
public string nationality { get; set; }
}
and in context model i define the relationship between model class and db table as follow

public System.Data.Entity.DbSet<****.Models.np_user_dtls> np_user__dtls { get; set; }
now i need an another class in which arabic name is not mandatory

so i create a model like below

public partial class np_user_dtls_1
{


[Display(Name = "First Name*")]
[Required(ErrorMessage = "First name is required.")]
public string user_name { get; set; }


[Required(ErrorMessage = "last name is required.")]
public string user_last_name { get; set; }


[Required(ErrorMessage = "nationality is required.")]
public string nationality { get; set; }
}
but when i create an entry for this in db context like below

public System.Data.Entity.DbSet<****.Models.np_user_dtls_1> np_user__dtls { get; set; }
its giving me an error

The type ****.Models.***** already contains a definition for np_user__dtls
is it not possible to define multiple model for a single table?
Sreekanth Mothukuru 6-Dec-15 12:37pm    
I think you need to create another view model class without validation annotations!!

1 solution

Only one model class can be defined on a table. Also, both classes np_user_dtls and np_user_dtls1 are same and having same validations. So, could you please clarify the need of two different classes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900