0

I know how to create Foreign Key in sql express and how to use dbml file and it's entities, But I like to change the name of a variable in one of my entities.

Let me explain this:

Lets think I have a Table named "Person" and this table has a Foreign Key to itself like parent and child. Now in C# I can use this syntax to get father of a child:

Person child = getSomePerson();
Person father = child.Person;

But I like to do something Like this:

Person child = getSomePerson();
Person father = child.father;

So what should I do to change default name "Person" To what I like it to be?

Edit 1:

It is better if I can do it with a sql query in sql server so I dont have to change anything in my C# project.

amir.sh
  • 193
  • 6
  • 18

1 Answers1

1

I'd not really recommend it, but if you insist, you can edit the dbml file itself (the XML) in a text editor and change the name of the reference property.

You'll see a line like (guessing a bit)

<Association Name="Person_Person" Member="Person" ThisKey="ParentId" OtherKey="PersondD" Type="Person" IsForeignKey="true" />

Now change Member="Person" into Member="Father". (I'd use Pascal case, not "father").

Then open the dbml file in designer mode (the default way), press "Save" (to trigger code generation) and the navigation property will have been changed.

Note that this change will obviously be gone if you'd have to regenerate your dbml in the future (because of db changes). You may consider moving to Entity Framework, where making such changes is natural.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Thanks for your answer. So there is no good way to do this at all? And what do you mean by move to Entity-Framework? – amir.sh Nov 30 '14 at 20:32
  • Well, this is not a terribly bad way, it's just a bit hacky, is all. And you may have to do it again in the future. If renaming navigation properties is important enough I think I'd do the same thing. By moving to EF I mean start using EF instead of LINQ-to-SQL. Both are pretty similar in the basic architecture so it may be possible to do this without too much refactoring. – Gert Arnold Nov 30 '14 at 21:29
  • Fair enough, Thank you. Now I should find a way to migrate from LINQ to EF. – amir.sh Dec 01 '14 at 00:40