且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该将EF实体和数据注释放在asp.net mvc +实体框架项目中

更新时间:2023-02-15 20:40:07


应用数据实体+ edmx文件(由EF4生成)进入单独的类库吗? / p>

IMHO所有数据访问逻辑是特定于某些数据访问技术(在您的实例框架中)应该进入单独的程序集。应该没有引用的MVC特定程序集。



在您的ASP.NET MVC应用程序中,您将引用此程序集并编写视图模型。那些将包含任何MVC特定属性的视图模型。然后,您可以在模型类和将传递给视图的视图模型之间进行映射。 AutoMapper 可用于促进此任务。


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

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.