where should I put the EF entity and data annotations in asp.net mvc + entity framework project

早过忘川 提交于 2019-12-10 12:16:56

问题


So I have a DataEntity class generated by EntityFramework4 for my sqlexpress08 database. This data context is exposed via a WCF Data Service/Odata to silverlight and win forms clients.

Should the data entities + edmx file (generated by EF4) go in a separate class library?

The problem here then is I would specify data annotations for a few entities and then some of them would require specific MVC attributes (like CompareAttribute) so the class library would also reference mvc dlls. There also happen to be entity users which will be encapsulated or wrapped into an IIdentity in the website. So its pretty tied to the mvc website. Or Should it maybe go in a Base folder in the mvc project itself?

Mostly the website is data driven around the database, like approve users, change global settings etc. The real business happens in the silverlight and win forms apps.

Im using mvc3 rc2 with Razor.
Thanks


回答1:


Should the data entities + edmx file (generated by EF4) go in a separate class library?

IMHO all data access logic which is specific to some data access technology (in your case Entity Framework) should go into a separate assembly. There should be no MVC specific assemblies referenced there.

In your ASP.NET MVC application you would then reference this assembly and write view models. It is those view models that will contain any MVC specific attributes. Then you could map between your model classes and those view models which would be passed to the view. AutoMapper could be used to facilitate this task.



来源:https://stackoverflow.com/questions/4496526/where-should-i-put-the-ef-entity-and-data-annotations-in-asp-net-mvc-entity-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!