1

I'm using the database-first approach while setting my first steps in Entity Framework. In my SQL Server database I have two tables, Location and Area.

Both have an Id column and the Location table already has an AreadId property. I would like to use this property for creating a one-to-many association between both tables (many Location entries for one Area entry), so I have created the following in my EDMX diagram using the EDMX-designer:

EDMX diagram screenshot

In the automatically generated Location.cs file, it looks as follows:

public partial class Location
{
    ...
    public int AreaId { get; set; }
    ...
    public virtual Area Area { get; set; }
}

I would like to use the already existing property Location.AreaId as the foreign key. How can I do that, using EDMX-designer?

Please keep in mind that I can modify the EDMX diagram, but not the automatically generated Location.cs file.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    Have you tried with this approach? https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/relationships?redirectedfrom=MSDN – Dave Miller Sep 27 '21 at 11:40
  • 1
    You don't really need both though; you can reference `Location.Area.Id` as the FK value. This is generally considered the better approach anyway. – psiodrake Sep 27 '21 at 13:50

0 Answers0