2

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:

  1. Remove the existing entity .edmx and .tt folders and re-create the mapping again
  2. Manually apply the modification to the model classes under the .tt folder after modifying the database
  3. 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

John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

2

If you right-click in the EDMX editor, there should be an option to "Update Model from Database." If you have removed fields in the database, you may still need to remove them, or even remove their table and re-add it via the "Update Model from Database" menu, but I find this much easier than deleting the whole edmx and starting fresh each time.

Update

As Ladislav says, you should never mess with auto-generated code files. As you have discovered, those code files get rewritten automatically. The creators of Entity Framework foresaw the issue you're having, and they provided a mechanism whereby you can add to those classes without modifying the auto-generated .cs file: partial classes.

Partial classes allow you to create class definitions that span multiple files, provided they are all in the same namespace and assembly. So alongside the autogenerated Context.cs file, you can create additional files like this:

Book.cs

[MetadataType(typeof(Books_validation))]
public partial class Book
{
    public bool IsManagedBy(string userName)
    {
        return ManagedBy.Equals(userName,
        StringComparison.OrdinalIgnoreCase);
    }
}

The presence of this separate definition of the partial class will cause this code to be effectively "merged" with the auto-generated Book class when the assembly is compiled, but it allows you to have this special custom code in a separate file which won't be affected when you regenerate the model data from the EDMX file.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • i tried to remove a field from a Data base table then i use the "Update Model from Database" and i select the table which I had modified, but i noted that the visual studio has recreated the model class which represents the modified table and all the classes that are connected to this table by FK relation and also all the data annotation and helper methods which i created inside the classes under the .tt folder were removed... so how i can avoid these two problems (affecting the other tables & keeping the data annotation and the helper methods). BR – John John Jan 30 '12 at 23:17
  • @johnG: I've read your comments a few times over, and I'm afraid I don't understand what you're saying. How is it affecting other tables? What helper methods are you losing? Please update your question with some code to show some examples of what you're talking about. – StriplingWarrior Jan 31 '12 at 00:01
  • thanks StriplingWarrior,, i have edited my post to make it clearer. BR – John John Jan 31 '12 at 03:58
  • thanks for ur reply; but the DBContext template will auto generate partial classes under the .tt folder, so if i try to add a new partial class, as this:- "public partial class Book ". the visual studio will raise an error that the class already exsists... BR – John John Jan 31 '12 at 17:43
  • @johnG: Have you tried it? What is the exact error you're getting? – StriplingWarrior Jan 31 '12 at 18:01
  • if i try to add the helper method at the end of the Book.cs partial class, then the helper method will be removed as the whole Book.cs file will be cleared and regenerated... so i think i can not get where should i define the Helper method as u mentioned in ur above edit. – John John Jan 31 '12 at 22:02
  • @johnG: If you define the partial class in a *separate file* (not the one that gets regenerated), then it will work. The .tt file will only regenerate one file: it won't touch other files that you create. I was assuming your auto-generated file was called something like "Context.cs", so I told you to create a new file (Book.cs) to house this new partial class definition. Because the class definition is partial, it won't matter that there are two definitions in different files: they will be merged into one class when you compile. – StriplingWarrior Jan 31 '12 at 22:37
  • 1
    ok it worked fine ,, i named the new file Book2.cs so i will not get an error that the class already exsist,, then inside the Book2.cs i define "public partial class Book .... BR – John John Feb 01 '12 at 00:14
0

You should never modify auto generated code. If you do it you have spoiled the whole workflow anyway because any single change to EDMX file can delete your changes. All custom code belongs to your own partial part of the class (and it is for example used for adding validation data annotations).

Once you follow correct workflow you will be able to modify EDMX and use Update from database which is the only correct way to push changes from database into your code.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • but in the DBContext template case which i am using,the DbContext will auto generate partial classes in the .tt folder. so i will not be able to define new partial classes for the model object, as the visual studio will raise an error that the class already exsists !!!. BR – John John Jan 31 '12 at 15:24