How to name foreign keys with database first approach
Asked Answered
E

1

6

Is there a way to change the naming convention with Entity Framework?

Example :

I have 2 tables

Planification
--------------
creator_id <fk>
guest_id <fk>


Profile
--------------
profile_id <pk>

creator_id and guest_id are both foreign keys for the profile_id

by default, entity would generate a planification class like this

public string guest_id { get; set; }
public string creator_id { get; set; }

public virtual Profile Profile { get; set; }
public virtual Profile Profile1 { get; set; }

I would prefer something more specific than Profile and Profile1 like Guest and Creator.

Is there a way to change the naming convention because I feel really guilty letting it like this.

Einhorn answered 25/7, 2014 at 14:49 Comment(0)
D
8

In your edmx you can rename the nav properties by clicking on the property and changing the Name.

enter image description here

Note that if you delete the table and build it from the database again you will have to rename it in the edmx.

If you are sufficiently annoyed by this. A way to get around it is to instead use a partial class with a property that gives a name to the default-named property.

public partial class Planification
{
    public Profile Creator 
    { 
        get{ return this.Profile1; }
        set{
            this.Profile1 = value;
        } 
    }

    public Profile Guest 
    { 
        get{ return this.Profile; }
        set{
            this.Profile = value;
        } 
    }
}
Doralynne answered 25/7, 2014 at 15:8 Comment(13)
That's what I thought, no easy way. I will go with the partial class. it's a brilliant idea. Thank you!Einhorn
How do I have access to Profile1 and Profile in the new partial class?Einhorn
Put the partial class in the same namespace as your model class.Doralynne
Do realise that "Profile1" can change context if another foreign key is added and you recreate the edmx.Dandiprat
@Dandiprat yes but only if the order of your foreign keys change in your table.Doralynne
@Shoe Yes, but it makes it very dangerous. If at a certain point, a foreign key is not needed anymore and deleted from the table. All references below that foreign key will shift. Depending on how how you used them it may break your whole application without even giving a compile error. Your approach will keep the code changes on 1 place though.Dandiprat
@Dandiprat I disagree on it being "very dangerous" but yes a problem can happen if you're changing the keys around. You should be doing your duty on testing your application when making changes. I think this solution is much more manageable than renaming properties in the edmx which are hard deleted when a table is regenerated. If you have a better solution I'm all ears.Doralynne
@Shoe 'it can happen' can be disastrous if your businesslogic suddenly uses another foreign key without you noticing without any build error or warning. just because somewhere else something changed. If I had the time I'd write a postprocessing script that updates the edmx xml. Changing the names based on the foreign column name.Dandiprat
Duly noted. If you have a better approach please make another answerDoralynne
@Dandiprat have you found any solution to this problem ? I even tried modifying the generation script which worked great, but EF crashed during runtime when performing database operationsShowpiece
Thanks for this solution! What'd be great is if someone had the ability to modify the .tt script that creates these - if we had access to the Association Dependent property Name, that would fix all of this.Marcelenemarcelia
@SteveStokes At this point I would consider moving to code-first since edmx support will be going away.Doralynne
@Shoe We have on most of our projects, but some cannot be upgraded to core yet, or for a while :(Marcelenemarcelia

© 2022 - 2024 — McMap. All rights reserved.