TheColonel26
TheColonel26

Reputation: 2728

Extend a model to add methods

I have reverse engineered a MySQL database in a C# desktop app. What I would like to do is extend one of the model classes, so that I can add methods to it to use locally in my application. I don't want to change any properties or anything just. Just get information and calculate things.

The problem is that when I inherit from one of the model classes I get an error about a new discriminator field being in the class but not the database.

Is there a way to do what I want to do?

Upvotes: 1

Views: 1485

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1502376

Given that the model classes are partial, you can just declare your own partial classes to join them:

// Note - needs to be in the same namespace as the auto-generated declaration

public partial class Foo
{
    // Add your own methods here, which can refer to the properties declared
    // for the same type in the auto-generated code
}

The point of partial classes is that multiple files can contribute source to the same type.

Upvotes: 4

nkvu
nkvu

Reputation: 5841

You could try extension methods to accomplish this instead of inheriting and creating a new subtype.

You would "attach" the extension methods to the model class which you generated.

Upvotes: 0

Related Questions