开发者

ASP MVC3 Database-first

开发者 https://www.devze.com 2023-02-27 05:18 出处:网络
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

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 o开发者_StackOverflowf 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.
0

精彩评论

暂无评论...
验证码 换一张
取 消