0

I want to override a setter of a property in a partial class:

Partial class:

public partial class User
{
    public string Name
    {
        set
        {
            // Do something
        }
    }
}

but I have the following error:

The type 'User' already contains a definition for 'Name'

How can I modify the initial setter generated in the DBML file?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Anas
  • 5,622
  • 5
  • 39
  • 71
  • I suggest checking out EF's Fluent API: http://stackoverflow.com/questions/6332340/what-is-entity-framework-fluent-api – niaher Aug 25 '12 at 15:24

2 Answers2

1

You can not override something in partial class. To override something you need first to derive from something.

Partial class is the same class but destributed between different IO files.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • And defining the same property of a partial class in 2 different files is also not possible. (right?) – niaher Aug 25 '12 at 15:18
  • 1
    @niaher I'll answer for Tigran. That's correct, it's not possible. Partial class is one and the same class, "partial" means physically splitting class code among separate files for human convenience (partial classes are often used by code generators, just to save the generator the need of jumping around your code every time it's rewriting his). And nothing more. So you can put these parts together into one single file again and it's logically 100% identical class as before. So, obviously, ambiguous identifiers can't be allowed. – Konrad Morawski Aug 25 '12 at 16:19
  • 1
    @niaher: if you want define different property, just define a *different one*, cause in *this* case we are talking about the same class, the same type. – Tigran Aug 25 '12 at 17:04
1

It is possible, just by employing some trickery

See http://www.codeproject.com/Articles/31519/LINQtoSQL-Customize-the-Code-Generated-by-the-Desi

This question could be of interest for you, too: DBML customization vs regeneration

You have not specified what sort of customization you want to achieve, though.

Community
  • 1
  • 1
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91