[英]How to get rid of Entity Framework attributes in auto-generated models?
When generating a model from a table in Visual Studio, I get these attributes that I don't really feel that I have any use for. 从Visual Studio中的表生成模型时,我得到的这些属性我并没有真正的用处。 Is there a way to remove them?
有没有办法删除它们?
public partial class TableModel
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
...
Also another thing I wonder, if I've added a couple of custom properties to the model that do not exist as columns in the table, will they be removed next time I update the model? 我还想知道的另一件事是,如果我向模型中添加了几个自定义属性,这些自定义属性不存在于表中的列中,那么下次我更新模型时会删除它们吗? When I've added a column to the table, eg?
当我在表格中添加一列时,例如?
You shouldn't modify these classes generated by Visual Studio. 您不应该修改Visual Studio生成的这些类。 They are generated as partial classes, so you can create a new partial class and add your custom properties there like this:
它们作为子类生成,因此您可以创建一个新的子类并在其中添加自定义属性,如下所示:
public partial class TableModel
{
public int Foo { get; set; }
}
These should be in the same namespace as the generated classes. 它们应与生成的类位于同一名称空间中。 This way you don't have to deal with the annotations you don't want to see, and if you regenerate your model your custom properties won't be deleted.
这样,您就不必处理不想看到的注释,并且如果重新生成模型,则不会删除自定义属性。
If you want to delete the annotations anyway, they can be easily deleted using Replace in files.. in VS (either by providing the exact text or a regular expression if there are many different annotations). 如果仍要删除注释,则可以使用VS中的Replace in files ..(通过提供确切的文本或如果有许多不同的注释则提供正则表达式)来轻松删除它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.