gpt4 book ai didi

asp.net - INSERT语句与FOREIGN KEY约束冲突

转载 作者:行者123 更新时间:2023-12-04 04:15:37 26 4
gpt4 key购买 nike

我是一个绿色的asp.net开发人员,并且在我的项目中使用了最新的 Entity Framework 。我在使用自动生成的值播种数据库时遇到问题(我认为)。这是确切的错误。

enter image description here

这是代码:

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();
}

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

最佳答案

您尝试播种的“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();
}

希望这足以让您接受。如果对addressId的查询不起作用,请告诉我,我们将很乐意为您提供帮助。

关于asp.net - INSERT语句与FOREIGN KEY约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42492996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com