简体   繁体   中英

ASP MVC3 Database-first

I use the entity framework for application ASP MVC3. At first I using code-first approach. I created the classes and used attributes to validate the data field

public class Person
{
    public int ID { get; set; }
    [Required(ErrorMessage = "Name can not be empty")]
    public string Name { get; set; }
}

But when using database-fitst, I do not know how to validate the datafields. In this case class Person is automatically created. How to do validate of its data fields?

Here's my $0.02 worth. If you want to validate your model which has been generated by entity framework using the Database first approach then you have to make use of a concept called 'Buddy' class. I believe Scottgu has a great article on that. As you can see the model classes generated by Entity Framework are partial classes meaning to say you can also create your own partial class to hold the so called attributes or to describe the metadata of the generated model. These partial classes will then be combined to form one class at runtime. Please do check out ScottGu's blog here: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

Hope this answers your question.

Hard to understand what exactly you mean, but I can recommend reading this .

The concept of Code First is simple:

  1. You create the classes. In your classes you can use the Required attribute just like you would with the normal Entity Framework
  2. EFCodeFirst creates the database tables for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM