简体   繁体   English

如何摆脱自动生成模型中的Entity Framework属性?

[英]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.

相关问题 如何在Entity Framework中为自动生成的类添加自定义方法? - How to add custom method to auto-generated class in Entity Framework? 循环进入类元数据中的自定义属性(以避免通过实体框架工作图修改自动生成的类) - Looping into custom attributes in class Metadata (to avoid modifying auto-generated class by Entity FrameWork diagram) C#按实体框架向自动生成的表中添加一列 - C# Add a column to Auto-Generated Table by Entity Framework 继承自Entity Framework中的自动生成的类 - Inheriting from auto-generated classes in Entity Framework 如何使用实体框架数据库首先自动生成的代码中的接口 - How to use interface from entity framework database first auto-generated code 如何更新Entity Framework自动生成的代码以获取存储过程的结果 - How to update the Entity Framework auto-generated code for the results of a stored procedure 如何修改自动生成的实体框架DbContext属性? - How do I modify an auto-generated Entity Framework DbContext Property? 如何使用 LINQ 连接多个表,包括连接表(由实体框架自动生成) - How can I join multiple tables including a join-table (auto-generated by Entity Framework) using LINQ 派生自动生成的实体类 - Derive Auto-Generated Entity Class 如何获取VC#来重建自动生成的.cs文件? - How to get VC# to rebuild auto-generated .cs files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM