[RESOLVED]Entity framework, DB first approach, update code based on changes in DB but saving modifications made by progammer

I updated my database(added a new table), and used Entity framework to propagate changes to the code.  It worked alright except all my modifications e.g. attributes I added to some classes  were undone. Is there a way to keep the changes made by the programmer
and still do updates using Entity Framework? In other words, prevent some model classes from getting updated and only update certain classes?

Thanks,

Rose

Yes, if you look closely at the generated work from EF you’ll see almost everything is a partial class which means you can add stuff in a partial that will not get overwritten.  Consider the EF part a project of it’s own that’s solely dedicated to the DB/Application
layer interface, look at it as a Mediator and NOT an implementation of other concerns.  It’s sole purpose it to 1) Map DB relationships and 2) To be a close if not exact model of the DB layer.  You can create classes there that inherit from other classes or
implement interfaces or even contain other Entity types, but make sure to create a folder and keep all the generated stuff separate from your custom stuff.

Then in the application project all you need to do is reference the entity project to write your queries.  Some take this even a step further by creating what they call a "repository", but for simplicity in get started just put all your queries in a folder
in application project.  I often make them all static methods so the upper layers of the application (such as a viewmodel) can do things like this:

DataLayer.GetCustomers();
DataLayer.GetCustomer("somelastname");
DateLayer.SaveCustomer(customerinstance);
...

You can see the simplicity at the upper app. layers of this pattern…

One other note, and a personal choice for me, I handle all exceptions in the data layer, not in the Entity project.  Why?  Because its the application that wants the data, let the applications’ data layer do error handling.  

What you want to do is create Meta Data classes for the tables that have fields you want to add attributes to.  For example, say you have a table called County and this is the class that Entity Framework generated for you:

public partial class County
    public int CountyId { get; set; }
    public string Name { get; set; }
}

Create another file called CountyMetaData.  Make sure it has the same namespace as the County class:

[MetadataType(typeof(CountyMetaData))]
public partial class County
{
    // Here you can define any additional properties that are not mapped to your database,
    // such as calculated fields.
}
       
public class CountyMetaData
{
    // Here re-define any properties that you want to add attributes to.

    [Display(Name = "County")]
    [MaxLength(50)]
    [Required(ErrorMessage = "*")]
    public string Name { get; set; }
}

When Entity Framework propagates the changes, it won’t touch the CountyMetaData class, so your attributes will not be lost.

Leave a Reply