엔티티 클래스를 생성하기 위해 SQL Server 데이터베이스에 연결하는 ADO.Net 엔티티 데이터 모델을 사용하고 있습니다.
이 속성을 사용하여 생성 된 클래스의 부분 클래스를 작성합니다.
[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
파일 (생성 된 파일)은 다음과 같습니다.
public partial class Employee
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Employee()
{
this.EmpWrkDesignations = new HashSet<EmpWrkDesignation>();
}
public int id { get; set; }
//public string badge_no { get; set; }
//public string first_name { get; set; }
//public string last_name { get; set; }
public System.DateTime row_added_date { get; set; }
public Nullable<System.DateTime> row_changed_date { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EmpWrkDesignation> EmpWrkDesignations { get; set; }
}
틀린 MetadataTypeAttribute
사용하고 있습니다. 특수 효과에 대한 별도의 클래스를 만들어야합니다.
나는 예를 들어 개인적인 내부 클래스를 사용하여 그것을한다.
[MetadataType(typeof(Table1MetaData))]
public partial class Table1
{
private class Table1MetaData
{
[MaxLength(20)]
public string Foo { get; set; }
}
}
중요한 것은 MetaData 클래스의 속성 이름이 부분 속성과 동일하다는 것입니다. 그래서 위와 같습니다 :
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
사용하십시오. 즉, Entity Framework에서 코드를 생성하는 방식에 만족하지 않으면 T4 템플릿을 편집 할 수 있습니다. edmx 파일 뒤에 숨겨져 있습니다 (그냥 펼치십시오).
당신은 물어 보지 않았지만, 당신은 이전에 또는 나중에 나왔습니다 :
함정은 맞춤 속성이 있고이를 가져오고 싶다면 메타 데이터도 확인해야합니다.
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;
}
이를위한 MVC
대안은 맞춤형 DataAnnotationsModelMetadataProvider
입니다.