0

I generated my .edmx from SQL Server and I get a confused point.

For example, I have a UserProfile table and its columns contain Creator, Modifier and Deleter.

But in my auto generated .cs file, only contains the relationship like User1, User2 and User3.

I know that I can rename the User1 to Creator by changing the table's name property in .edmx setting, but I'm afraid what if User1 is not actually map the Creator but the Modifier or Deleter?

Now I use my tables' relationship by knowing that the foreign key obey the order of that in SQL Server and work fine, but I really want to know how it works.

Is there any way to determine the foreign table name in advance when I modify the SQL Server tables? Like I can modify the name when create relationship in SQL Server.

Below is an example of my table's auto-generated model:

public partial class UserProfile
{
    public long Creater { get; set; }
    public long Modifier { get; set; }
    public long Deleter { get; set; }
    ...

    ...
    public virtual User User { get; set; }
    public virtual User User1 { get; set; }
    public virtual User User2 { get; set; }
}

I have seen this question that partially match my point, but I need it can be determine by SQL Server table foreign key setting because it will back to default when I regenerate the .edmx.

My English is not good, please don't mind.

Any suggestion is appreciated.

Terry Wei
  • 1,521
  • 8
  • 16
  • 3
    Open the EDMX diagram, right click on `User1` and select properties. See what the FK is and then name it accordingly. – CodingYoshi Nov 05 '18 at 03:19
  • @CodingYoshi I know that strategy, but if there any way to make the same effect by changing something in SQL Server table setting? – Terry Wei Nov 05 '18 at 03:20
  • Like set the table's foreign key name in SQL Server and the foreign key name in .edmx will change from `User1` to `Modifier`. – Terry Wei Nov 05 '18 at 03:23
  • If I set in .edmx, that will come back to default when I regenerate again, isn't it? – Terry Wei Nov 05 '18 at 03:25
  • No there is not. EF only looks at the FK and then creates properties and calls them the name of the referenced table. If multiple, it suffixes with a number. – CodingYoshi Nov 05 '18 at 03:27
  • yes you lose the changes if you drop the item from the edmx, then regenerate. welcome to edmx `hell`. time to look at code-first? – jazb Nov 05 '18 at 03:27
  • Oh damn, that's what I afraid to hear. BTW, if there's no way to do that, should I close this question? – Terry Wei Nov 05 '18 at 03:30
  • you can edit the edmx file directly if you're brave (foolish?) enough :-) – jazb Nov 05 '18 at 03:37
  • there are 3rd party tools that will do the job for you with edmx files - they make their coin in this space – jazb Nov 05 '18 at 03:39
  • OK, I won't do that. I have no trouble when using `User` and `User1` but try to find a better way to show them in code. Thanks a lot. – Terry Wei Nov 05 '18 at 03:39
  • The navigation property name *will not* be changed when you do `Update model from database` by right-click on the design surface of the edmx file. Regenerating the edmx from scrach (or even one entity) is seldom required, and will also lose any other customizations you have made, so you should be certain there is no other way to achieve what you want. – Tsahi Asher Nov 05 '18 at 16:14
  • sometimes for convinience I would like to regenerate the table instead of modifying the content. For example, edit the column name. – Terry Wei Nov 06 '18 at 00:21

0 Answers0