Generating Interfaces from entity framework database first auto-generated code

一曲冷凌霜 提交于 2020-01-20 04:35:05

问题


I am using MVC3, C# 4.0 and Entity Framework in Visual Studio 2010. I am generating my edmx and Designed.cs files from a database. I am then generating interfaces from the entities in the Designer.cs file as part of my nLayer structure.

The original code is

public partial class DataEntrySummary : EntityObject

which then becomes

public partial class DataEntrySummary : EntityObject, Mb.Interface.IDataEntrySummary

My concern is that when the database changes (and it will) and I regenerate the edmx files I will lose all the interface definitions.

Is there a better way of achieving the same result without having to regenerate the interfaces.

Thank you


回答1:


EF generates the classes with the partial keyword so that you can add extra functionality to the entities by creating another file and place the interface specific stuff there.

public partial class DataEntrySummary : Mb.Interface.IDataEntrySummary
{
}

These files will not get affected when EF updates the model.




回答2:


You are heading to the right direction. But for retaining the interfaces after every EDMX refresh, you'll need to customize the T4 files.

You can take a look at the customized T4 files from https://entityinterfacegenerator.codeplex.com/ They generate 1 interface for each of your class so that you can easily mock and test them separately.

So, everytime you add a new table or field, you can simply execute their T4 templates along with the ones provided by Microsoft for generating EF types.

Regards.




回答3:


I think that creating an event on model update so that each time the model updates it will add the interface definitions.

Another option is to create a proxy class that implements a specific interface and inherits from the model.



来源:https://stackoverflow.com/questions/12348734/generating-interfaces-from-entity-framework-database-first-auto-generated-code

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