Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase-first approach and modifying the database schema
    primarykey
    data
    text
    <p>I am building an ASP.NET MVC web application using entity framework DbContext using the database first approach.</p> <p>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:</p> <ol> <li>Remove the existing entity .edmx and .tt folders and re-create the mapping again</li> <li>Manually apply the modification to the model classes under the .tt folder after modifying the database</li> <li>Neither of these two options are valid.</li> </ol> <p>How should I approach the task of modifying my database schema?</p> <p><strong>Edited</strong> For example i have the following partial class which were automatically created under the .tt folder, baring in mind that i have added the <strong>*<em>IsManagedBy</em>*</strong> helper methodand the [MetadataType(typeof(Books_validation))] to it :-</p> <pre><code>[MetadataType(typeof(Books_validation))] public partial class Book { public Book() { this.Assessments = new HashSet&lt;Assessment&gt;(); this.Users_Books = new HashSet&lt;Users_Books&gt;(); } public int BookID { get; set; } public string BookName { get; set; } public string ManagedBy { get; set; } public byte[] Timestamp { get; set; } public virtual ICollection&lt;Assessment&gt; Assessments { get; set; } public virtual User User { get; set; } public virtual ICollection&lt;Users_Books&gt; Users_Bookes { get; set; } public bool IsManagedBy(string userName) { return ManagedBy.Equals(userName, StringComparison.OrdinalIgnoreCase); } } </code></pre> <p>And then i create the Book_validation class to apply the data annotations as following:-</p> <pre><code>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; } } </code></pre> <p>This approach is causing three problems:-</p> <p><strong>1.</strong> 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 !!!.</p> <p><strong>2.</strong> 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.</p> <p><strong>3.</strong> 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.</p> <p>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</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload