使用数据库优先方法时无法找到模型和上下文类文件
我是 stackoverflow 和 .Net MVC 3 的新手。
我正在使用数据库优先方法在 .Net 中构建 MVC 3 Web 应用程序。我创建了 .edmx 文件,Visual Studio 通过该文件自动生成模型类和上下文类文件。
现在我想在我的应用程序中进行一些验证,但我无法在“Models”文件夹中找到自动生成的模型类。虽然我可以将验证放入 .edmx 文件的 Designer.cs 文件中,其中公共部分类出现在我的应用程序“UserDetails”表中。
请澄清,我如何找到这些自动生成的模型类文件,或者告诉我我尝试的方法是否是将验证放入 .edmx 文件的 Designer.cs 文件中的好习惯?
I am new to stackoverflow and for .Net MVC 3 as well.
I am building a MVC 3 web-application in .Net by using database first approach. I created the .edmx file through which Visual Studio has generated the model class and context class files automatically.
Now I want to put some validations in my application, but I am unable to locate the auto generated model classes in the "Models" folder. Though I am able to put the validations in designer.cs file of my .edmx file, where the public partial class appears for my applications "UserDetails" table.
Please clarify, that how can I locate those auto-generated model class files, or tell me if the approach which I tried is a good practice to put the validations inside the designer.cs file of my .edmx file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自动生成的模型类文件位于 .designer.cs 文件中。它们是作为分部类生成的,并且应该通过创建您自己的分部类来分离业务逻辑。否则,当从模型重新生成类时,自动生成将覆盖您的代码。
一个建议是,如果您正在使用实体框架,DbContext 和 Code First (POCO) 是您应该考虑的第一个功能。而且 DbContext 比 ObjectContext 使用起来简单得多,并且可以满足最常见的开发需求。
如果您是 EF 新手,请按照 ado.net 团队使用 DbContext 的演练进行操作。 (EF 4.1 模型和数据库首次演练)。
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
(MVC 中的“Models”文件夹实际上是视图模型,必须根据视图自行创建。)
Auto-generated model class files are located in .designer.cs file. These are generated as partial class and should separate out business logic by creating your own partial class. Otherwise, autogeneration will override your code when regenerate the class from model.
One advice, if you are approaching Entity Framework, DbContext and Code First (POCO) are the first features you should consider. And DbContext is much simpler to use than ObjectContext and will serve the most common development needs.
If you are new with EF, follow the walkthrough from ado.net team which is using with DbContext. (EF 4.1 Model & Database First Walkthrough).
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
("Models" folder in MVC is actually view models and have to create yourself base on the view.)
您需要根据想要呈现视图的方式创建模型,并且验证也在那里(您也可以说 ViewModel 部分)。将验证放在 Designer.cs 文件中并不是一个好的做法。
使用 S#arp Lite 架构
You need to create models on basis of how you want to render views and validations also goes there (Also you can say ViewModel section).It is not good practice to put validations inside designer.cs file.
The best practice to make separation in MVC using S#arp Lite Architecture