简体   繁体   English

在EF Core中存储一对一关系

[英]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. 我试图将两个实体保存到相互关联的数据库中,但是其中一个实体从不保存另一个实体的ID。

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: 我有一个OpeningTimes课:

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. 将其保存到数据库后,将填充Locations表中的OpeningTimeId列,但不填充OpeningTimes表中的LocationId列。

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. 现在,在此特定示例中,我不一定需要填充LocationId列,但是我确实需要对其他实体执行此操作,并且我不确定为什么这不起作用。

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. 您在类Location中未定义类型为Int和Identifier And主键的字段ID,一旦将记录添加到表中,该键就会自动添加一个单位。 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. 对于一对一映射,您可以向表中的Referrerd外键列添加唯一约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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