INSERT语句与FOREGIGN KEY约束冲突

Som*_*ive 9 asp.net entity-framework entity-framework-core

我是一个绿色的asp.net开发人员,我正在使用我的项目的最新实体框架.我有一个问题是使用自动生成的值为我的数据库播种(我认为).这是确切的错误.

在此输入图像描述

这是代码:

public class Address
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AddressId { get; set; }
// some more properties
}

public class ApplicationUser : IdentityUser
{
        [ForeignKey("Address")]        
    public int AddressId { get; set; }
    public Address Address { get; set; }

// some more properties
}

public class Shelter
{
        [Required]
        [ForeignKey("Address")]
        public int AddressId { get; set; }
        public Address Address { get; set; }
// some more properties
}
//seed
private void CreateShelters()
{
    EntityEntry<Address> MspcaAddress = DbContext.Addresses.Add(new Address()
    {
                Street = "350 S Huntington Ave",
                City = "Jamaica Plain",
                State = "MA",
                AreaCode = "02130",
                Latitude = 42.3228928,
                Longititude = -71.11120540000002
    });
     EntityEntry<Address> BostonAnimalCareAndControlAddress = DbContext.Addresses.Add(new Address()
     {
                Street = "26 Mahler Rd",
                City = "Roslindale",
                State = "MA",
                AreaCode = "02131",
                Latitude = 42.2943377,
                Longititude = -71.12153390000003
            });
            EntityEntry<Address> AnimalRescueLeagueOfBostonAddress = DbContext.Addresses.Add(new Address()
            {
                Street = "10 Chandler St",
                City = "Boston",
                State = "MA",
                AreaCode = "0S2116",
                Latitude = 42.3470486,
                Longititude = -71.06976929999996
            });

            EntityEntry<Shelter> Mspca = DbContext.Shelters.Add(new Shelter()
            {
                Name = "MCSPA",
                AddressId = MspcaAddress.Entity.AddressId
            });
            EntityEntry<Shelter> BostonAnimalCareAndControl = DbContext.Shelters.Add(new Shelter()
            {
                Name = "Boston Animal Care And Control",
                AddressId = BostonAnimalCareAndControlAddress.Entity.AddressId
            });
            EntityEntry<Shelter> AnimalRescueLeagueOfBoston = DbContext.Shelters.Add(new Shelter()
            {
                Name = "Animal Rescue League Of Boston Address",
                AddressId = AnimalRescueLeagueOfBostonAddress.Entity.AddressId
            });
            DbContext.SaveChanges();
   }
Run Code Online (Sandbox Code Playgroud)

我尝试重新创建数据库以查看是否存在数据/表不一致的问题,但我仍然收到错误.有任何想法吗?

Dan*_*Dev 4

Shelter您尝试播种的“ ”实体要求您提交现有的AddressID您上面的代码尝试获取尚未创建的地址的 AddressId。

您必须首先将您的地址提交到数据库(使用 DbContext.SaveChanges();

然后,您可以使用检索到的 AddressId 创建 Shelter 实体。下面的代码显示了如何执行此操作的示例,但您可能必须重新编写获取 addressId 的查询。或者,您可能必须将它们与插入过程完全分开。

    private void CreateShelters()
{
    EntityEntry<Address> MspcaAddress = DbContext.Addresses.Add(new Address()
    {
                Street = "350 S Huntington Ave",
                City = "Jamaica Plain",
                State = "MA",
                AreaCode = "02130",
                Latitude = 42.3228928,
                Longititude = -71.11120540000002
    });
     EntityEntry<Address> BostonAnimalCareAndControlAddress = DbContext.Addresses.Add(new Address()
     {
                Street = "26 Mahler Rd",
                City = "Roslindale",
                State = "MA",
                AreaCode = "02131",
                Latitude = 42.2943377,
                Longititude = -71.12153390000003
            });
            EntityEntry<Address> AnimalRescueLeagueOfBostonAddress = DbContext.Addresses.Add(new Address()
            {
                Street = "10 Chandler St",
                City = "Boston",
                State = "MA",
                AreaCode = "0S2116",
                Latitude = 42.3470486,
                Longititude = -71.06976929999996
            });

    DbContext.SaveChanges();


            EntityEntry<Shelter> Mspca = DbContext.Shelters.Add(new Shelter()
            {
                Name = "MCSPA",
                AddressId = MspcaAddress.Entity.AddressId
            });
            EntityEntry<Shelter> BostonAnimalCareAndControl = DbContext.Shelters.Add(new Shelter()
            {
                Name = "Boston Animal Care And Control",
                AddressId = DbContext.Addresses.Where(x => x.Street == "26 Mahler Rd").FirstOrDefault().AddressId
       });
            EntityEntry<Shelter> AnimalRescueLeagueOfBoston = DbContext.Shelters.Add(new Shelter()
            {
                Name = "Animal Rescue League Of Boston Address",
                AddressId = DbContext.Addresses.Where(x => x.Street == "10 Chandler St").FirstOrDefault().AddressId

            });
            DbContext.SaveChanges();
   }
Run Code Online (Sandbox Code Playgroud)

希望这足以让您度过难关。如果对 addressId 的查询不起作用,请告诉我,我将很乐意帮助您解决这些问题。