0

I use the Model Designer of Visual Studio to generate my object classes. Now I want to adjust the generated classes with inheritance and getter/setter changes.

If the Model Designer for example has created the class Browser:

public partial class Browser
{
    public Browser(){}    
    public int Id { get; set; }
    public string Name { get; set; }
}

I want to adjust the generated code to

public partial class Browser : ValidatableModel, IFormattable
{
    public string name;


    public Browser(){ name = "" }    
    public Guid Id { get; private set; }
    public string Name 
    {
        get { return name; }
        set { SetPropertyAndValidate(ref name, value); }
    }
}

I'm using the WPF Application Framework which is implementing the database in another way than I want to.

Do you know a solution for this or any other approach to use Model First approach in WPF? I think there has to be one.

10ff
  • 813
  • 4
  • 16
  • 31

1 Answers1

0

You shouldn't change the generated code; any updates to your model will wipe them out. You need to change your model, then push those changes to your database.

The custom getter / setters can be implemented in a partial class. Your new generated code should look like this:

public partial class Browser
{
    public string name;


    public Browser(){ }    
    public Guid Id { get; set; }
    public string Name {get; set;}
}

Then you add a partial class like this:

public partial class Browser : ValidatableModel, IFormattable
{
    public string name;

    public override Browser(){ name = "" }    
    public override Guid Id { get; private set; }
    public override string Name 
    {
        get { return name; }
        set { SetPropertyAndValidate(ref name, value); }
    }
}
Joe Brunscheon
  • 1,949
  • 20
  • 21
  • Can't do this, because the compiler says that "Browser" already contains a definition for "Id" and "Name". Do you know another way? – 10ff Aug 23 '13 at 12:30
  • Wasn't sure if that would work or not because of that. It was worth a shot. The only other way I know of doing something like this would be to create classes that are essentially copies of your entity classes, and then do complicated mapping back to entities before you save off to the database. So retrieve entities -> Map to Objects. Saving: take objects -> map to entities -> save. I DON'T recommend doing this as you'll then have to figure out how to determine if an "object" is modified and handle all the state that you get with entity framework. – Joe Brunscheon Aug 23 '13 at 20:16