11

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

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice, I completely scanned over the partial keyword and didn't even realise ...
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.

2 Comments

Thanks for this, sadly too late as we have now moved to code first. I will certainly review this again if there is a further need.
Sure thing. Thanks for checking out the project. Hope it could help people like us who had to decide which technology to use. IMO code first is great only for new projects. Database first seems more practical. That's why I build the project to help people who could not afford to do code first.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.