Manually adapt DbFirst model

I have project using EF with a DbFirst model. Some properties of an entity need to be adjusted to fit the needs of the project. I did this by editing the .edmx Designer file manually. These adjustments are minor string manupulations. The problem is: Everytime the model is updated from the database, these changes are overwritten. Is there another way to do this?

Answers


Depending on what you're wanting to do, EF has alot of 'hooks' available to you so you can add custom code, not in the designer. All the classes are partial, so you can create a partial class, and then hook into the OnYourPropertyNameChanging if want to alter the setter.

Excerpt from some of my code:

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String EmployeeComment
{
    get
    {
        return _EmployeeComment;
    }
    set
    {
        OnEmployeeCommentChanging(value);
        ReportPropertyChanging("EmployeeComment");
        _EmployeeComment = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("EmployeeComment");
        OnEmployeeCommentChanged();
    }
}
private global::System.String _EmployeeComment;
partial void OnEmployeeCommentChanging(global::System.String value);
partial void OnEmployeeCommentChanged();

So, if you wanted to do custom logic in the Changing method you could declare a partial class, and put your code as follows:

public partial class Comment
{
  partial OnEmployeeCommentChanging(string value)
  {
     /* your code goes here */
  }
}

Not really, except doing he modifications from the database so these changes will remain stagnant, otherwise create an event on model update so each time your model is updated, your settings will be reinforced.


That is right when you update the model from database it gets refreshed. You need to create a seperate class where you can define your entities and you can customize this.


The other two answers cover most of what you need.

Your entity models are all built as partial classes. Each time you do anything to the database you change the class definition. That is why they all have the header that says DON'T CHANGE THIS CODE!

Assuming you have a data project that your data is in, I create new classes in the same project. So if your model is

partial class Customer:EntityObject

Then you would create a CustomerOverload class library that is defined as

partial class Customer

And that class would then contain your additions etc. Just don't add methods, those won't carry over to the client.


Need Your Help

Android: Drawing to Canvas While in Loop

android list loops android-canvas draw

I'm playing around with Canvas to get the feel of it, so I created an array of Points that I then tried to draw to the Canvas. However, only the very last Point shows up on the canvas...

Youtube iframe wmode issue

iframe youtube wmode

Using javascript with jQuery, I am adding an iframe with a youtube url to display a video on a website however the embed code that gets loaded in the iframe from youtube doesnt have wmode="Opaque",

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.