简体   繁体   English

如何保持属性属性插入到ASP.NET MVC中自动生成的文件中

[英]How to keep properties attributes inserted into auto-generated files in ASP.NET MVC

I use ASP.NET MVC 5 and Entity Framework 6. So I have generated some files under *.tt 我使用ASP.NET MVC 5和Entity Framework6。因此,我在* .tt下生成了一些文件。

I would like to keep attributes that i inserted into those files because I have created many pages automatically based on Entity Framework classes. 我想保留插入这些文件中的属性,因为我已经基于Entity Framework类自动创建了许多页面。

But when I update Entity Framework model from the database I loose everything that I inserted. 但是,当我从数据库更新Entity Framework模型时,我会松开插入的所有内容。

So my question is how to keep it from deleting? 所以我的问题是如何防止其删除?

//------------------------------------------------------------------------------ // // This code was generated from a template. // ------------------------------------------------ ------------------------------ // //此代码是从模板生成的。 // // Manual changes to this file may cause unexpected behavior in your application. // //对此文件进行手动更改可能会导致应用程序发生意外行为。 // Manual changes to this file will be overwritten if the code is regenerated. //如果重新生成代码,则对该文件的手动更改将被覆盖。 // //------------------------------------------------------------------------------ // // ---------------------------------------------- --------------------------------

namespace MyWebSIte.DataModel
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class ItemMovement
    {
        public System.Guid ID { get; set; }

        [DataType(DataType.DateTime)]  <---- I would like to keep it.
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Fecha")]
        public System.DateTime? Changed { get; set; }

Those files are not meant to be edited manually. 这些文件不是要手动编辑的。 You should take advantage of the fact that those classes are partial and can be "extended". 您应该利用这些类是partial并且可以“扩展”的事实。 Take a look at Metadata classes , they will allow you to annotate properties. 看一下Metadata类 ,它们将允许您注释属性。

The generated class ItemMovement is a partial class. 生成的类ItemMovement是局部类。
This allows you to write a second partial class which is marked with the necessary data annotations. 这样,您就可以编写第二个局部类,并在其中标记必要的数据注释。
In your case the partial class ItemMovement would look like this: 在您的情况下,子类ItemMovement如下所示:

   namespace MyWebSIte.DataModel
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;

        [MetadataType(typeof(ItemMovementMetaData))]
        public partial class ItemMovement
        {
            public System.Guid ID { get; set; }        
            public System.DateTime? Changed { get; set; }
        }

        public partial class ItemMovementMetaData {

            [DataType(DataType.DateTime)]
            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
            [Display(Name = "Fecha")]
            public System.DateTime? Changed { get; set; }   

            //....................
        }
    }

You can use the codefirst which gives u max control over your models and dbcontext. 您可以使用codefirst,它可以最大程度地控制您的模型和dbcontext。

Step1 : Create a codefirst model using the existing database. 步骤1:使用现有数据库创建一个代码优先模型。 codefirst model using the existing database 使用现有数据库的codefirst模型

Step 2: now type enable-migrations in the package manager console 步骤2:现在在程序包管理器控制台中键入enable-migrations

Step 3: You use the (db)context and the model generated from the database table. 步骤3:您使用(db)上下文和从数据库表生成的模型。 make changes to your model 修改模型

Step 4: type add-migration [some name to identify the migration] in the package manager console 步骤4:在程序包管理器控制台中键入add-migration [某些名称以标识迁移]

Step 5: check the generated migration file. 步骤5:检查生成的迁移文件。

Step 6: type update-database in the package manager console 步骤6:在套件管理员控制台中输入update-database

Now your changes are updated to the database. 现在,您的更改已更新到数据库。 From now on you can us the codefirst approach to handle the changes to your database. 从现在开始,您可以使用codefirst方法来处理对数据库的更改。

暂无
暂无

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

相关问题 如何在自动生成的视图中使ASP.NET MVC 3呈现具有复杂类型的对象? - How do I make ASP.NET MVC 3 render objects with complex types in the auto-generated views? 将DataAnnotations添加到自动生成的DBML类? MVC 2.0 ASP.NET - Adding DataAnnotations to auto-generated DBML Class? MVC 2.0 ASP.NET 将用户定向到 ASP.Net MVC 应用程序中的另一个页面会导致自动生成的代码出现语法错误 - Directing user to another page in ASP.Net MVC application leads to syntax error in auto-generated code 在MVC自动生成的类中注释asp.net属性 - Annotate asp.net properties in MVC auto generated class ASP.Net自动生成的管理工具 - ASP.Net auto-generated Admin tool 如何将文件和属性发送到ASP.NET MVC中的控制器,并以二进制形式接收动态生成的PDF作为响应? - How to send files and attributes to controller in ASP.NET MVC and receive a dynamically generated PDF as Binary as response? ASP.net 4.5将当前自动生成的MachineKey复制到另一台服务器 - ASP.net 4.5 copy the current auto-generated MachineKey to another server 如何摆脱自动生成模型中的Entity Framework属性? - How to get rid of Entity Framework attributes in auto-generated models? ASP.NET MVC 5 中自动生成的 EDMX class 的构造函数 - Constructors for Auto Generated EDMX class in ASP.NET MVC 5 在使用 visual studio 2019 运行应用程序时,如何防止在 Asp.Net Core API 3.1 中自动生成 web.config? - How to prevent auto-generated web.config in Asp.Net Core API 3.1 while running the application using visual studio 2019?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM