-6

I have a sql table called 'Notes', which I need to add for it a Model:

public class Note
{
    public int ID { get; set; }
    public string Note { get; set; }
    public string UserName { get; set; }
    public DateTime Date { get; set; }
}

I'm running into an issue with the Note member, I can't name it Note because member names cannot be same as their enclosing type, altering the table is also not an option- it's being used by another production website.

On the project's requirements classes names have to be consistent- the name should represent a single instance of the sql table, for example: Documents table -> Document model etc..

Basically the Note table should not have a Note column but that's it and I have to deal with.

Is there a way for a member name to be different than the SQL column name using Dapper?

user3378165
  • 6,546
  • 17
  • 62
  • 101
  • 2
    Well, I don´t know actually what a note should be and what it is supposed to do. But I agree compiler, a `Note` should probably not have a `Note`. We can´t answer this question, it´s fairly opinion-based. – MakePeaceGreatAgain Jan 30 '19 at 07:56
  • 1
    Change the name of the class or the property. Not that many options. What exactly do you want us to tell you? There might be a problem with renaming either of them, but then you have to show us the problem. – Patrick Hofman Jan 30 '19 at 07:56
  • I would pick a more generic name for the field itself, e.g. `Title` or `Content`, if altering the table is an option. – Sven Mich Jan 30 '19 at 07:59
  • Clearly that is not possible, so you have to go for a plan B. Which option that is is up to you. – Patrick Hofman Jan 30 '19 at 08:06
  • @HimBromBeere please see my edited question. – user3378165 Jan 30 '19 at 08:08
  • 1
    To stay on your `Document`-example you should ask yourself why an instance of `Document` should have a `Document`-member? Doesn´t make much sense, IMHO. A `document` however may have `Content` for example, as your `Note` may have a `Text` or similar. – MakePeaceGreatAgain Jan 30 '19 at 08:08
  • Your edit doesn't help. Saying "I don't want to" doesn't suddenly give us the possibility to alter the working of the compiler. You have to pick one option. Period. – Patrick Hofman Jan 30 '19 at 08:08
  • @HimBromBeere, you are totally right, but I'm not the one that structured the `Note` table, it's an old database. – user3378165 Jan 30 '19 at 08:09
  • Well, than change what you **can** change, which is your sourcecode. You can change the names in your program without having to change anything on your database, see Damiens answer. – MakePeaceGreatAgain Jan 30 '19 at 08:10
  • I was looking for ideas like the one that @Damien_The_Unbeliever wrote. Don't think the down votes are fair here. – user3378165 Jan 30 '19 at 08:11
  • 1
    But you didn't state any problem. Just "I don't want to". For us to give you help to fix a problem, we should know your problem. – Patrick Hofman Jan 30 '19 at 08:12
  • @PatrickHofman, HimBromBeere please see again my edited question. – user3378165 Jan 30 '19 at 08:20
  • "My question is if a member names must match the SQL column name?" It depends how you are reading the data. But, no, it doesn't. How are you reading the data actually? – Patrick Hofman Jan 30 '19 at 08:20
  • Now that I edited my question, please explain the downvotes. – user3378165 Jan 30 '19 at 08:22
  • 1
    "Is there a way for a member name to be different than the SQL column name?" Yes it is. Depending on your ORM you may add attributes to your member that indicate that on DB it is named differentel.y Which ORM are you using? – MakePeaceGreatAgain Jan 30 '19 at 08:24
  • *Are* you even using ORM, or are you just creating the objects yourself in a loop, assigning the values loaded from database? There is nothing in this question we can help you with. – Patrick Hofman Jan 30 '19 at 08:25
  • 1
    Why is that not in the question? It is a vital piece of information! – Patrick Hofman Jan 30 '19 at 08:26
  • I guess there are many duplicates, like this one: https://stackoverflow.com/questions/40703015/custom-mapping-in-dapper – Patrick Hofman Jan 30 '19 at 08:27
  • 1
    Or this: https://stackoverflow.com/questions/8902674/manually-map-column-names-with-class-properties – MakePeaceGreatAgain Jan 30 '19 at 08:27

1 Answers1

6

You don't have to make the member names match the SQL column names. If you're using some kind of ORM, there's almost always a way to specify the mapping explicitly. For ADO.Net, obviously you're free to use the names as appropriate.

I'd rename the member NoteText. I'd also consider applying the same change in SQL, simply because it more accurately reflects that this is (apparently) but one component of something you're calling Note.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448