ASP数据库MVC3先 [英] ASP MVC3 Database-first

查看:92
本文介绍了ASP数据库MVC3先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的应用程序ASP MVC3实体框架。起初,我用code-第一种方法。我创建的类和使用的属性来验证数据字段

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; }
}

但在使用数据库时fitst,我不知道如何验证数据域。
在这种情况下,类人是自动创建。如何做好其数据字段的验证?

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?

推荐答案

下面是我的$ 0.02的价值。如果您想验证已通过实体框架使用数据库第一种方法生成模型,那么你必须使用一个叫做好友类的概念。我相信Scottgu有一个伟大的文章。正如你可以看到实体框架生成的模型类是部分类意思就是说,你也可以创建自己的局部类举行所谓的属性或描述生成的模型的元数据。然后,这些部分类将被结合,形成在运行一个类。请不要检查出ScottGu的博客
此处的http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

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.

这篇关于ASP数据库MVC3先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆