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