Jason
Jason

Reputation: 11615

Adding Interfaces to DataContext Classes

I am using Linq-To-SQL and I would like to attach an interface to each of my entities.

I can edit the designer.cs file and accomplish this.

However, when I make a changes to the dbml through the UI it rewrites the whole designer.cs and I lose my changes.

Am I just going to have to deal with it, or is there a way to get around it?

I am doing this in the designer.cs file(the IMatchable is a custom interface of mine):

public partial class Error : INotifyPropertyChanging, INotifyPropertyChanged, IMatchable
{
   ...
}

Upvotes: 1

Views: 1093

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063774

Don't edit the designer file; the beauty of partial classes is that you can create a separate file with just

public partial class Error : IMatchable
{ }

(assuming that we are using implicit interface implementaion by virtue of having properties that match the required interface)

Small word of caution though: if you are using VS2008 and you have MyClasses.dbml and MyClasses.designer.cs, do not call this file MyClasses.cs - there is a bug in VS2008 that makes this a nuisance (you have to keep moving the using directives inside the namespace, or the code-generator breaks) - fixed in VS2010 though.

Also, if it was a single interface, that every type in your model implemented, you can cheat by specifying that at the object base-type in the DBML. The designer doesn't show this option, but if you edit the DBML manually it works fine.

Upvotes: 5

Related Questions