views:

132

answers:

1

Hi,

Is it possible to override an attibute set on a partial class?

So I have one autogenerated partial class like this:

[Table(Name="dbo.Users")]
    public partial class MbsUser : INotifyPropertyChanging, INotifyPropertyChanged
    {

This is generated in my DBML. The problem is, I don't want my class to use this table. I've created a view called "dbo.ActiveUsers" and would like this to be used instead (to keep out deactivated users).

I've tried creating another partial class with the same attribute as follows:

[Table(Name = "dbo.MbsUsersActive")]
public partial class MbsUser : IEquatable<MbsUser>
{

But I get the error:

Duplicate 'Table' attribute

Any help?

Thanks!

-Ev

+1  A: 

You get that error because an attribute can control whether there are one or many intances of that attribute in the [AttributeUsage] declaration, and it declares only one. No, that isn't going to work unfortunately...

Why don't you change the mapping in the designer to point to the new entity? Don't know if that will work the same, but its worth a try.

Brian
Yeah, that's what I've done now.(Thanks so much for the fast reply by the way).It works if I change it in the designer, but if the DBML is generated again, I'll lose that change, and get all users, not just active users.Any ideas how I could make it persist? Thanks again!
Ev
You mean its not saving your changes, or are you saying how to deal with if you need to delete the model and recreate it? I've not had issues with the former, and the latter is always a concern; try not to delete the entire model but only update the tables you need to (can easily find the table using the properties window drop down). If you need to modify that table, make the change manually (add the column, add in the mapping details).
Brian
The latter. If the DBML gets recreated by another developer in the future, the view won't get used, and deactivated users will suddenly become active.Thanks again! Appreciate the help!
Ev
P.S. I've marked this as answered, because you've answered my original question (thanks) but if you have any ideas on this I'll be really happy! I might raise another question that asks this more specifically.
Ev
Thank you. The only thing I can say is that is a fundamental question for a lot of things, even development, is how to inform others of some deviation, whether code, DB, or here too with DBML. I think there is a description field in the entity you could put maintenance notes (requires they check that first)... make sure its well documented and there is a procedural document that people follow when updating the model... I don't know that you can do it via code or an attribute....
Brian
Thanks for the advice Brian.What I might do is rename the table object in the DBML and change it the code to use the new class name. This way if it's re-generated from teh wrong table, at least it'll error. Thanks again for your help mate.
Ev