Here is the solution I have been using for such cases. It is useful when you have auto-generated classes that you want to decorate with attributes. Let's say this is the auto-generated class:
public partial class UserProfile
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
And let's say, I would like to add an attribute to specify that UserId is the key. I would then create a partial class in another file like this:
[Table("UserProfile")]
[MetadataType(typeof(UserProfileMetadata))]
public partial class UserProfile
{
internal sealed class UserProfileMetadata
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…