0
是否有任何方式可以通过DataAnnotations(如Required,StringLength)在model.edmx中生成数据库对象?从数据库中的EF设计师与DataAnnotations
是否有任何方式可以通过DataAnnotations(如Required,StringLength)在model.edmx中生成数据库对象?从数据库中的EF设计师与DataAnnotations
当您对EF使用数据库优先方法时,所有类都生成为partial
类。这意味着您可以将定义分散在两个或更多源文件中。 如果更改生成类,它将被覆盖。
假设你有一个分部类的EF生成的用户作为
public partial class User
{
public string Username { get; set; }
public string Telphone { get; set; }
}
现在使用DataAnnotations
上的User
的属性,你会在同一个命名空间创建具有相同名称的新源文件,使使用MetadataType
属性为:
[MetadataType(typeof(UserMetaData))]
public partial class User
{
public string Username { get; set; }
public string Telphone { get; set; }
}
public class UserMetaData
{
[Required]
[Display(Name = "User name")]
public string Username { get; set; }
[DataType(DataType.PhoneNumber)]
[Display(Name = "Telephone number")]
public string Telephone { get; set; }
}
正如你可以看到我用MetadataType
在指定的元数据类与数据模型类相关联的第二个例子属性。
这是可能去使用t4模板生成它吗? – AggieR
@AggieR你也可以看看** [this](https://t4metadatatemplate.codeplex.com/)** codeplex for t4模板项目 – Izzy