简体   繁体   中英

Storing One-To-One relationships in EF Core

I'm trying to save two entities into a database that relate to each other, but one of the entities never saves the ID of the other.

I have a Location class:

public string Name { get; set; }
public string Description { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public Company Company { get; set; }

[ForeignKey("OpeningTimeId")]
public OpeningTime OpeningTimes { get; set; }

And I have an OpeningTimes class:

public string Monday { get; set; }
public string Tuesday { get; set; }
public string Wednesday { get; set; }
public string Thursday { get; set; }
public string Friday { get; set; }
public string Saturday { get; set; }
public string Sunday { get; set; }

[ForeignKey("LocationId")]
public Location Location { get; set; }

I'm then trying to save these to the database as such:

Location location = new Location()
{
    Name = LocationDto.Name,
    Description = LocationDto.Description,
    Latitude = LocationDto.Latitude,
    Longitude = LocationDto.Longitude,
    Company = LocationDto.Company,
    OpeningTimes = new OpeningTime() {
        Monday = LocationDto.OpeningTimes.Monday,
        Tuesday = LocationDto.OpeningTimes.Tuesday,
        Wednesday = LocationDto.OpeningTimes.Wednesday,
        Thursday = LocationDto.OpeningTimes.Thursday,
        Friday = LocationDto.OpeningTimes.Friday,
        Saturday = LocationDto.OpeningTimes.Saturday,
        Sunday = LocationDto.OpeningTimes.Sunday,
    }
};

_context.Locations.Add(location);
_context.SaveChanges();

When this gets saved to the database, the OpeningTimeId column in the Locations table is populated, but the LocationId column in the OpeningTimes table is not.

Now in this specific example I don't necessarily need to populate the LocationId column, but I do need to do this for other entities, and I'm not sure why this isn't working.

You are in class Location did not define Field ID Of Type Int And Identifier And Primary key that Automatically add one unit as soon as the record is added to the table. But you can do this in the database in the table as a wizard, but you need to add it to the classroom again, but if you want to add the attribute in the class itself, the compiler will do this behind the scenes.

like:

  [Key]
  public int LocationId {get;set;}
  public string Name { get; set; }
  public string Description { get; set; }
  public string Latitude { get; set; }
  public string Longitude { get; set; }
  public Company Company { get; set; }

  [ForeignKey("OpeningTimeId")]
  public OpeningTime OpeningTimes { get; set; }

There is no need to have reference in both the tables, remove from one table. For one to one mapping you can add a unique constraint to the referrerd foreign key column in the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM