C# 实体框架&x27;s自动生成的类';每次更新模型时都会重新生成s代码
我正在使用ADO.Net实体数据模型,该模型连接到SQL Server数据库以生成实体类 我正在使用此属性为生成的类创建分部类:C# 实体框架&x27;s自动生成的类';每次更新模型时都会重新生成s代码,c#,sql-server,asp.net-mvc,entity-framework,entity-framework-6,C#,Sql Server,Asp.net Mvc,Entity Framework,Entity Framework 6,我正在使用ADO.Net实体数据模型,该模型连接到SQL Server数据库以生成实体类 我正在使用此属性为生成的类创建分部类: [MetadataType(typeof(Employee))] 在字段上放置DataAnnotations。因此,我从生成的类(.g.cs)中删除所有字段,并将它们移动到另一个分部类中,并将数据注释放在这个类中 问题是,每当我从数据库更新模型时(无论我是首先从实体数据模型中删除现有表还是仅仅刷新模型),生成的类代码都会重新生成。我还必须从g.cs类中手动删除字段
[MetadataType(typeof(Employee))]
在字段上放置DataAnnotations
。因此,我从生成的类(.g.cs
)中删除所有字段,并将它们移动到另一个分部类中,并将数据注释放在这个类中
问题是,每当我从数据库更新模型时(无论我是首先从实体数据模型中删除现有表还是仅仅刷新模型),生成的类代码都会重新生成。我还必须从g.cs
类中手动删除字段属性,因为它们已经存在于其他部分类中(在由MetadataType
属性指定的类中)
当我更新模型时,生成的代码是否可能不会重新生成?如果需要,我是否可以为特定的表/类手动重新生成代码
[MetadataType(typeof(Employee))]
public partial class Employee
{
public string badge_no { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
[Display(Name="Name")]
public string FullName { get { return first_name + " " + last_name; } }
[Display(Name = "Badge-Name")]
public string NameAndBadge { get { return badge_no + " " + FullName; } }
}
这是g.cs
文件(生成的文件)
公共部分类员工
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公职人员()
{
this.EmpWrkDesignations=new HashSet();
}
公共int id{get;set;}
//公共字符串标记_no{get;set;}
//公共字符串first_name{get;set;}
//公共字符串last_name{get;set;}
public System.DateTime行\添加了\日期{get;set;}
公共可空行\u已更改\u日期{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection EmpWrkDesignations{get;set;}
}
您使用的MetadataTypeAttribute
错误。您应该为注释创建一个单独的类
我使用一个私有的内部类来实现它,例如:
[MetadataType(typeof(Table1MetaData))]
public partial class Table1
{
private class Table1MetaData
{
[MaxLength(20)]
public string Foo { get; set; }
}
}
重要的是,元数据类的属性名与部分类的属性名相同。因此,上面的属性名与:
public partial class Table1
{
[MaxLength(20)]
public string Foo { get; set; }
}
你应该选第一个
此外,由于该类是局部的,而不是:
public string first_name { get; set; }
public string last_name { get; set; }
[Display(Name="Name")]
public string FullName { get { return first_name + " " + last_name; }
您可以而且应该只写您的财产:
[Display(Name="Name")]
public string FullName { get { return first_name + " " + last_name; }
结论:
您没有问,但您会在较早或稍后遇到: 一个陷阱是,如果您拥有自定义属性,并且想要获取它们,那么还需要检查元数据:
public static TAttribute GetAttributeOrUseMetadata<TAttribute>(this PropertyInfo element, bool inherit = false) where TAttribute : System.Attribute
{
//Find normal attribute
var attribute = element.GetCustomAttribute<TAttribute>(inherit);
if (attribute != null)
{
return attribute;
}
//Find via MetadataTypeAttribute
if (element.DeclaringType != null)
{
var metadataType = element.DeclaringType.GetCustomAttribute<MetadataTypeAttribute>(inherit);
var metadataPropertyInfo = metadataType?.MetadataClassType.GetProperty(element.Name);
return metadataPropertyInfo?.GetCustomAttribute<TAttribute>();
}
return null;
}
公共静态TatAttribute GetAttributeOrUseMetadata(此PropertyInfo元素,bool inherit=false),其中TatAttribute:System.Attribute
{
//查找法线属性
var attribute=element.GetCustomAttribute(继承);
if(属性!=null)
{
返回属性;
}
//通过MetadataTypeAttribute查找
if(element.DeclaringType!=null)
{
var metadataType=element.DeclaringType.GetCustomAttribute(继承);
var metadataPropertyInfo=metadataType?.MetadataClassType.GetProperty(element.Name);
返回metadataPropertyInfo?.GetCustomAttribute();
}
返回null;
}
MVC的备选方案是自定义。您使用的
MetadataTypeAttribute
错误。您应该为注释创建一个单独的类
我使用一个私有的内部类来实现它,例如:
[MetadataType(typeof(Table1MetaData))]
public partial class Table1
{
private class Table1MetaData
{
[MaxLength(20)]
public string Foo { get; set; }
}
}
重要的是,元数据类的属性名与部分类的属性名相同。因此,上面的属性名与:
public partial class Table1
{
[MaxLength(20)]
public string Foo { get; set; }
}
你应该选第一个
此外,由于该类是局部的,而不是:
public string first_name { get; set; }
public string last_name { get; set; }
[Display(Name="Name")]
public string FullName { get { return first_name + " " + last_name; }
您可以而且应该只写您的财产:
[Display(Name="Name")]
public string FullName { get { return first_name + " " + last_name; }
结论:
MetadataType
仅用于将DataAnnotation属性添加到生成的属性您没有问,但您会在较早或稍后遇到: 一个陷阱是,如果您拥有自定义属性,并且想要获取它们,那么还需要检查元数据:
public static TAttribute GetAttributeOrUseMetadata<TAttribute>(this PropertyInfo element, bool inherit = false) where TAttribute : System.Attribute
{
//Find normal attribute
var attribute = element.GetCustomAttribute<TAttribute>(inherit);
if (attribute != null)
{
return attribute;
}
//Find via MetadataTypeAttribute
if (element.DeclaringType != null)
{
var metadataType = element.DeclaringType.GetCustomAttribute<MetadataTypeAttribute>(inherit);
var metadataPropertyInfo = metadataType?.MetadataClassType.GetProperty(element.Name);
return metadataPropertyInfo?.GetCustomAttribute<TAttribute>();
}
return null;
}
公共静态TatAttribute GetAttributeOrUseMetadata(此PropertyInfo元素,bool inherit=false),其中TatAttribute:System.Attribute
{
//查找法线属性
var attribute=element.GetCustomAttribute(继承);
if(属性!=null)
{
返回属性;
}
//通过MetadataTypeAttribute查找
if(element.DeclaringType!=null)
{
var metadataType=element.DeclaringType.GetCustomAttribute(继承);
var metadataPropertyInfo=metadataType?.MetadataClassType.GetProperty(element.Name);
返回metadataPropertyInfo?.GetCustomAttribute();
}
返回null;
}
MVC
这是一种定制的替代方案。千万不要触摸生成的代码,因为它的性质是生成的,并且会覆盖您所做的任何更改。代码是由T4模板生成的,如果您愿意,您可以根据自己的需要进行调整。因此,是的,如果您愿意,您可以修改/调整EF T4模板不要在基本实体类上生成任何属性,因为您指定了元数据类中的所有内容