简体   繁体   English

扩展模型以添加方法

[英]Extend a model to add methods

I have reverse engineered a MySQL database in a C# desktop app. 我在C#桌面应用程序中反向设计了一个MySQL数据库。 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? 有办法做我想做的事吗?

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. 部分类的要点是多个文件可以为同一类型提供源。

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. 您可以将扩展方法“附加”到您生成的模型类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM