問候我是 ASP.NET 核心的新手,我在創建具有關系的資料庫時遇到問題客戶可能有多個地址(一對多關系)。僅針對一個原產國(一對一關系)
我嘗試建立一對多的關系而不是一對一的關系,它對相反的作用相同。我只是不明白如果我試圖讓它們都出現在我的資料庫背景關系中 FK 鍵的問題在哪里。錯誤 -
SqlException: INSERT 陳述句與 FOREIGN KEY 約束“FK_Address_Customer_CustomerId”沖突。沖突發生在資料庫“AdressesContext-da27b4d1-a732-4121-bb76-e3bd5633718f”、表“dbo.Customer”、“Id”列中。
我的物體
namespace Adresses.Models
{
public enum Gender
{
Female,Male
}
public class Customer
{
public int Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public DateTime Birthdate { get; set; }
public Gender Gender { get; set; }
public ICollection<Address> Addresses { get; set; }
}
}
國家模式
namespace Adresses.Models
{
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
}
namespace Adresses.Models
{
public enum Type
{
Billing,Delivery
}
public class Address
{
public int Id { get; set; }
public int CustomerId { get; set; }
public int CountryId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public Type Type { get; set; }
public Customer Customer { get; set; }
public Country Country { get; set; }
}
}
播種資料庫我正在使用這種方法
using Adresses.Models;
using System;
using System.Linq;
namespace Adresses.Data
{
public class DbInitializer
{
public static void Initialize(AdressesContext context)
{
context.Database.EnsureCreated();
// Look for any students.
if (context.Customer.Any())
{
return; // DB has been seeded
}
var customers = new Customer[]
{
new Customer{FullName="Carson Alexander",Birthdate=DateTime.Parse("2005-09-01"),Gender=Gender.Female,Email="[email protected]"},
new Customer{FullName="Meredith Alonso",Birthdate=DateTime.Parse("2002-09-01"),Gender=Gender.Male,Email="[email protected]"}
//new Customer{FullName="Arturo Anand",Birthdate=DateTime.Parse("2003-09-01"),Gender=Gender.Female,Email="[email protected]"},
//new Customer{FullName="Gytis Barzdukas",Birthdate=DateTime.Parse("2002-09-01"),Gender=Gender.Male,Email="[email protected]"},
//new Customer{FullName="Yan Li",Birthdate=DateTime.Parse("2002-09-01"),Gender=Gender.Female,Email="[email protected]"},
//new Customer{FullName="Peggy Justice",Birthdate=DateTime.Parse("2001-09-01"),Gender=Gender.Male,Email="[email protected]"},
//new Customer{FullName="Laura Norman",Birthdate=DateTime.Parse("2003-09-01"),Gender=Gender.Female,Email="[email protected]"},
//new Customer{FullName="Nino Olivetto",Birthdate=DateTime.Parse("2005-09-01"),Gender=Gender.Female,Email="[email protected]"}
};
foreach (Customer s in customers)
{
context.Customer.Add(s);
}
context.SaveChanges();
var countries = new Country[]
{
new Country{Id=1,Name="Heavens"},
new Country{Id=2,Name="Hell"}
};
foreach (Country e in countries)
{
context.Country.Add(e);
}
context.SaveChanges();
var addresses = new Address[]
{
new Address{CustomerId=1,StreetAddress="test",City="One City",Zip="2402",Type=Models.Type.Delivery,CountryId=1},
new Address{CustomerId=2,StreetAddress="another",City="City",Zip="2403",Type=Models.Type.Billing,CountryId=2},
new Address{CustomerId=2,StreetAddress="another",City="City",Zip="2403",Type=Models.Type.Delivery,CountryId=1}
};
foreach (Address e in addresses)
{
context.Address.Add(e);
}
context.SaveChanges();
}
}
}
uj5u.com熱心網友回復:
您插入的客戶沒有獲得 ID1和2,因此您的地址具有錯誤的CustomerId值。
不完全清楚為什么會發生這種情況 - 您之前是否從客戶表中插入和洗掉了記錄?
作為客戶的一部分插入地址通常更容易:
var countries = new Country[]
{
new Country{ Name="Heavens" },
new Country{ Name="Hell" }
};
foreach (Country e in countries)
{
context.Country.Add(e);
}
var customers = new Customer[]
{
new Customer
{
FullName = "Carson Alexander",
Birthdate = new DateTime(2005, 9, 1),
Gender = Gender.Female,
Email = "[email protected]",
Addresses = new[]
{
new Address
{
StreetAddress = "test",
City = "One City",
Zip = "2402",
Type = Models.Type.Delivery,
Country = countries[0]
}
}
},
new Customer
{
FullName = "Meredith Alonso",
Birthdate = new DateTime(2002, 9, 1),
Gender = Gender.Male,
Email = "[email protected]",
Addresses = new[]
{
new Address
{
StreetAddress = "another",
City = "City",
Zip = "2403",
Type = Models.Type.Billing,
Country = countries[1]
},
new Address
{
StreetAddress = "another",
City = "City",
Zip = "2403",
Type = Models.Type.Delivery,
Country = countries[0]
}
}
}
};
foreach (Customer s in customers)
{
context.Customer.Add(s);
}
context.SaveChanges();
uj5u.com熱心網友回復:
你必須解決國家關系
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Address> Addresses { get; set; }
}
并且由于您不知道 customerId,因此最好從國家/地區和地址開始添加。這樣資料庫服務器將自動分配主鍵和外鍵。
var addresses = new Address[]
{
new Address{StreetAddress="test",City="One City",Zip="2402",Type=Models.Type.Delivery,
Country = new Country{ Name="Heavens"},
Customer = new Customer {FullName="Carson Alexander",Birthdate=DateTime.Parse("2005-09-01"),Gender=Gender.Female,Email="[email protected]"}
},
new Address{StreetAddress="another",City="City",Zip="2403",Type=Models.Type.Delivery,
Country= new Country { Name="Hell"},
Customer= new Customer { FullName="Meredith Alonso",Birthdate=DateTime.Parse("2002-09-01"),Gender=Gender.Male,Email="[email protected]"}
}
};
context.Address.AddRange(addresses);
context.SaveChanges();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/379675.html
標籤:asp.net-mvc
