EF5:DB首先。生成的模型和自定义验证 [英] EF5: DB First. Generated Models and Custom Validation
问题描述
如果我使用第一代开发模式,我可以完全控制我的代码。_
例如,我有型号:User。
public class User {
[Key]
public int id {get;组; }
[StringLength(50,ErrorMessage =*长度可能少于50个符号)]
[必需(ErrorMessage =*登录不能为空)]
public string Login {get;组; }
[StringLength(50,ErrorMessage =*长度可能少于50个符号)]
[必需(ErrorMessage =*名称不能为空)]
public string FirstName {get;组; }
[StringLength(50,ErrorMessage =*长度可能少于50个符号)]
[必需(ErrorMessage =*姓氏不能为空)]
public string LastName {get;组; }
[DataType(DataType.EmailAddress)]
public string Email {get;组; }
public string Phone {get;组; }
[必需]
public DateTime CreatedAt {get;组; }
[必需]
public DateTime LastLogin {get;组; }
public List< Post> PostList {get;组; }
}
我有能力创建自己的验证,如果我使用 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>评论{get;组; }
public DbSet< Post>邮寄{get;组; }
public DbSet< User>用户{get;组;
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;组; }
public string Login {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string Email {get;组; }
public string Phone {get;组; }
public System.DateTime CreatedAt {get;组; }
public System.DateTime LastLogin {get;组; }
public virtual ICollection< Comment>评论{get;组; }
public virtual ICollection< Comment> Comment1 {get;组; }
public virtual ICollection< Post>邮寄{get;组; }
public virtual ICollection< Post> Post1 {get;组; }
public virtual User User1 {get;组; }
public virtual User User2 {get;组;
public partial class Post
{
public Post()
{
this.Comment = new HashSet< Comment> );
}
public int id {get;组; }
public string标题{get;组; }
public string Text {get;组; }
public string TextFormatted {get;组; }
public System.DateTime CreatedAt {get;组; }
public System.DateTime UpdatedAt {get;组; }
public Nullable< int> CreatedById {get;组; }
public Nullable< int> UpdatedById {get;组; }
public Nullable< int> UserId {get;组; }
public virtual User User {get;组; }
public virtual User User1 {get;组; }
public virtual ICollection< Comment>评论{get;组;
}
public partial class注释
{
public int id {get;组; }
public string标题{get;组; }
public string Text {get;组; }
public string TextFormatted {get;组; }
public System.DateTime CreatedAt {get;组; }
public System.DateTime UpdatedAt {get;组; }
public Nullable< int> CreatedById {get;组; }
public Nullable< int> UpdatedById {get;组; }
public Nullable< int> PostId {get;组; }
public virtual User User {get;组; }
public virtual User User1 {get;组; }
public virtual Post Post {get;组; }
public virtual注释1 {get;组; }
public virtual Comment Comment2 {get;组; }
}
问题:
1.据我所知如果我使用DB First Development Model,我不能使用我自己的模型进行数据访问,只是由ADO.NET Entity Data Model生成的模型/类??
我试图使用自己的模型UserOwn除了生成User,所以我得到一个错误无法检索TestDBFirst02.Models.UserOwn的元数据。期待。
2.我可以在一个项目中使用开发模型:Code First和DB First吗?
3.如果我需要使用生成的模型,我需要什么做,当我想使用我自己的验证?我试图修改生成的模型,它的作用是:
[StringLength(50,ErrorMessage =*长度可能小于50符号)]
[必需(ErrorMessage =*登录不能为空)]
public string Login {get;组; }
但是,如果我需要从DB更新模型,当然我的验证码将被ADO覆盖。 NET实体数据模型和属性消失。
回答<3>第三方问题:需要使用 [MetadataTypeAttribute(typeof(UserValidation)]
属性并创建验证类,所以代码是:
[MetadataType(typeof(UserValidation))]
public partial class User {}
public class UserValidation
{
[StringLength 50,ErrorMessage =*长度可能少于50个符号)]
[必需(ErrorMessage =*登录不能为空)]
public string Login {get; set;}
}
与相同!命名空间,因为用户模型。
阅读更多:
- http://www.asp.net/mvc/tutorials/mvc-5/database-第一次开发/增强数据验证
- http:// jnye.co/Posts/19/adding-validation-to-models-created-by-entity-framework-database-first-c
- http://weblogs.asp.net/scottgu/asp-net-mvc-2-模型验证
- 数据库首次验证
- 添加验证使用数据库优先模型(EF 5)建模
- http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first
If I use Code First Development Model, I have a full control of my code.
For Example, I have model: User.
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; }
}
I have ability to create my own validation, like at model "User".
In case, if I use DB First Development Model, I need to use ADO.NET Entity Data Model to generate models. I have 3 tables:
I have generated files:
DBContext.edmx
- DBContext.Context.tt
- DBContext.Designer.cs
- DBContext.edmx.diagram
- DBContext.tt
- Comment.cs
- DBContext.cs
- Post.cs
- Comment.cs
and code below:
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; }
}
Questions:
1. As I understand, if I use DB First Development Model, I can't use my own models for data access, just models/classes, generated by ADO.NET Entity Data Model?
I tried to use my own model "UserOwn" except generated "User", so I got an error "Unable to retrieve metadata for 'TestDBFirst02.Models.UserOwn'". Expected.
2. Can I use both Development Model: Code First and DB First inside one project?
3. If I need to use generated models, what I need to do, when I want to use my own validation? I tried to modificate generated model, and it works:
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
But, if I need to update model from DB, of course my validation code overwrites by ADO.NET Entity Data Model and attributes disappear. How can I overcome the situation with my own validation?
Answer the 3-rd question: need to use [MetadataTypeAttribute(typeof(UserValidation)]
attribute and create validation class, so code is:
[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; }
}
with the same! namespace, as User model has.
Read more:
- http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/enhancing-data-validation
- http://jnye.co/Posts/19/adding-validation-to-models-created-by-entity-framework-database-first-c
- http://weblogs.asp.net/scottgu/asp-net-mvc-2-model-validation
- Database First Validation
- Adding validation to model with Database First model (EF 5)
- http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first
这篇关于EF5:DB首先。生成的模型和自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!