如果我使用 Code First Development Model ,我可以完全控制我的代码 例如,我有模型:用户。
public class User {
[Key]
public int id { get; set; }
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Name can't be empty")]
public string FirstName { get; set; }
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Last name can't be empty")]
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public string Phone { get; set; }
[Required]
public DateTime CreatedAt { get; set; }
[Required]
public DateTime LastLogin { get; set; }
public List<Post> PostList { get; set; }
}
我有能力创建自己的验证,例如模型&#34;用户&#34;。
如果我使用 DB First Development Model ,我需要使用ADO.NET实体数据模型来生成模型。我有3张桌子:
我已生成文件:
DBContext.edmx
- DBContext.Context.tt
- DBContext.Designer.cs
- DBContext.edmx.diagram
- DBContext.tt
- Comment.cs
- DBContext.cs
- Post.cs
- Comment.cs
和以下代码:
public partial class DBContext : DbContext
{
public DBContext()
: base("name=DBContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Comment> Comment { get; set; }
public DbSet<Post> Post { get; set; }
public DbSet<User> User { get; set; }
}
public partial class User
{
public User()
{
this.Comment = new HashSet<Comment>();
this.Comment1 = new HashSet<Comment>();
this.Post = new HashSet<Post>();
this.Post1 = new HashSet<Post>();
}
public int id { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime LastLogin { get; set; }
public virtual ICollection<Comment> Comment { get; set; }
public virtual ICollection<Comment> Comment1 { get; set; }
public virtual ICollection<Post> Post { get; set; }
public virtual ICollection<Post> Post1 { get; set; }
public virtual User User1 { get; set; }
public virtual User User2 { get; set; }
}
public partial class Post
{
public Post()
{
this.Comment = new HashSet<Comment>();
}
public int id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string TextFormatted { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime UpdatedAt { get; set; }
public Nullable<int> CreatedById { get; set; }
public Nullable<int> UpdatedById { get; set; }
public Nullable<int> UserId { get; set; }
public virtual User User { get; set; }
public virtual User User1 { get; set; }
public virtual ICollection<Comment> Comment { get; set; }
}
public partial class Comment
{
public int id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string TextFormatted { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime UpdatedAt { get; set; }
public Nullable<int> CreatedById { get; set; }
public Nullable<int> UpdatedById { get; set; }
public Nullable<int> PostId { get; set; }
public virtual User User { get; set; }
public virtual User User1 { get; set; }
public virtual Post Post { get; set; }
public virtual Comment Comment1 { get; set; }
public virtual Comment Comment2 { get; set; }
}
问题:
1.据我所知,如果我使用DB First Development Model,我不能使用自己的模型进行数据访问,只使用由ADO.NET实体数据模型生成的模型/类?
我尝试使用自己的模型&#34; UserOwn&#34;除了生成&#34;用户&#34;,所以我收到错误&#34;无法检索&#39; TestDBFirst02.Models.UserOwn&#39;&#34;的元数据。预计。
2.我可以在一个项目中同时使用开发模型:Code First和DB First吗?
3.如果我需要使用生成的模型,当我想使用自己的验证时,我需要做什么?我试图修改生成的模型,它起作用:
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
但是,如果我需要从DB更新模型,当然我的验证代码会被ADO.NET实体数据模型覆盖并且属性会消失。我如何通过自己的验证来克服这种情况?
答案 0 :(得分:2)
回答第3个问题:需要使用[MetadataTypeAttribute(typeof(UserValidation)]
属性并创建验证类,因此代码为:
[MetadataType(typeof(UserValidation))]
public partial class User {}
public class UserValidation
{
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
}
使用相同!命名空间,因为用户模型具有。
阅读更多: