I am building an ASP.NET MVC web application using entity framework DbContext using the database first approach.
If under certain conditions I needed to modify the database such as adding new table or modifying an existing table (adding columns or changing a column data type), should I:
- Remove the existing entity .edmx and .tt folders and re-create the mapping again
- Manually apply the modification to the model classes under the .tt folder after modifying the database
- Neither of these two options are valid.
How should I approach the task of modifying my database schema?
Edited For example i have the following partial class which were automatically created under the .tt folder, baring in mind that i have added the *IsManagedBy* helper methodand the [MetadataType(typeof(Books_validation))] to it :-
[MetadataType(typeof(Books_validation))]
public partial class Book
{
public Book()
{
this.Assessments = new HashSet<Assessment>();
this.Users_Books = new HashSet<Users_Books>();
}
public int BookID { get; set; }
public string BookName { get; set; }
public string ManagedBy { get; set; }
public byte[] Timestamp { get; set; }
public virtual ICollection<Assessment> Assessments { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Users_Books> Users_Bookes { get; set; }
public bool IsManagedBy(string userName)
{
return ManagedBy.Equals(userName,
StringComparison.OrdinalIgnoreCase);
}
}
And then i create the Book_validation class to apply the data annotations as following:-
public class Books_validation
{
[Required(ErrorMessage = "Name is required")]
public string BookName { get; set; }
public string ManagedBy { get; set; }
[ConcurrencyCheck]
[Timestamp]
public Byte[] Timestamp { get; set; }
}
This approach is causing three problems:-
1. The IsManagedBy helper method cannot be defined in the Book_validation class and i should define it in the automatically generated Book partial class , which means it will be removed if i regenerate the code !!!.
2. If for example i modify the Assessment table which is related to the Book class (by Foreign key ) and then i chose the “Update Model from Database” option from the .edmx designer; then visual studio will also regenerate the Book class , which will cause more troubles for me.
3. Even the [MetadataType(typeof(Books_validation))] which i wrote in the automatically generated code will also be removed in case i regenerate the code, so this means that i have to go over all the modified classes and add the associated MetadataType(typeof) for them.
All these problems were not happening when i was using the ObjectContext template, since the ObjectContext template will not automatically generate partial classes , it will only generate model classes and then i can add partial classes and the metadatetype for them... so i think it is better to return back to ObjectContext instead of DBContext !!!, any suggestion on this and the above problems? BR