2

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:
enter image description here

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?

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.