所以,我的 ef 模型有以下初始化程序。
为简化起见,我有一个车辆、一个客户、一个地址实体。我用一种方法创建了地址实体。现在我需要创建客户实体,但如何将地址值分配给现有对象?
public class DevelopmentInitializer
{
/// <summary>
/// This class allows the database tables to be created and seed at runtime.
/// </summary>
public class AskAndTrackInitializer : DropCreateDatabaseAlways<AskAndTrackContext>
{
/// <summary>
/// This fills the database with sample data to start with
/// </summary>
/// <param name="context">DBContext required for the connection</param>
protected override void Seed(AskAndTrackContext context)
{
BuildDealers(context);
BuildRequesters(context);
BuildAddresses(context);
BuildCustomers(context);
BuildVehicles(context);
BuildWorkflowsBase(context);
}
private void BuildDealers(AskAndTrackContext context)
{
var dealer1 = new Dealer {DealerId = 1, Name = "Dealer 1"};
var dealer2 = new Dealer { DealerId = 2, Name = "Dealer 2" };
var dealer3 = new Dealer { DealerId = 3, Name = "Dealer 3" };
var dealer4 = new Dealer { DealerId = 4, Name = "Dealer 4" };
var dealer5 = new Dealer { DealerId = 5, Name = "Dealer 5" };
context.Dealers.Add(dealer1);
context.Dealers.Add(dealer2);
context.Dealers.Add(dealer3);
context.Dealers.Add(dealer4);
context.Dealers.Add(dealer5);
}
/// <summary>
/// Requesters test data
/// </summary>
private void BuildRequesters(AskAndTrackContext context)
{
var requester1 = new Requester {EmailAddress = "le.xx@hotmail.com", Name="Andres x", RequesterId=1};
var requester2 = new Requester { EmailAddress = "le.xxx@hotmail.com", Name = "Andres y", RequesterId = 2 };
var requester3 = new Requester { EmailAddress = "le.yyy@hotmail.com", Name = "Juan l", RequesterId = 3 };
var requester4 = new Requester { EmailAddress = "le.zzzz@hotmail.com", Name = "Peter x", RequesterId = 4 };
var requester5 = new Requester { EmailAddress = "le.aaaa@hotmail.com", Name = "Patrick z", RequesterId = 5 };
context.Requesters.Add(requester1);
context.Requesters.Add(requester2);
context.Requesters.Add(requester3);
context.Requesters.Add(requester4);
context.Requesters.Add(requester5);
}
/// <summary>
/// Workflow base test data and its related objects
/// </summary>
private void BuildWorkflowsBase(AskAndTrackContext context)
{
}
/// <summary>
/// Vehicle test data
/// </summary>
private void BuildVehicles(AskAndTrackContext context)
{
//var vehicle1
}
/// <summary>
/// Customers test data
/// </summary>
private void BuildCustomers(AskAndTrackContext context)
{
//var customer1 = new Customer{Address = ???
}
private void BuildAddresses(AskAndTrackContext context)
{
var address1 = new Address
{
AddressId = 1,
Box = "71",
City = "Antwerp",
Number = "1",
Street = "Belgielei",
Zip = "2018"
};
var address2 = new Address
{
AddressId = 2,
Box = "21",
City = "Antwerp",
Number = "1",
Street = "Lange Leemstraat",
Zip = "2018"
};
var address3 = new Address
{
AddressId = 3,
Box = "23",
City = "Antwerp",
Number = "1",
Street = "Lamoriniestraat",
Zip = "2018"
};
var address4 = new Address
{
AddressId = 4,
Box = "24",
City = "Antwerp",
Number = "1",
Street = "De Keyserlei",
Zip = "2000"
};
var address5 = new Address
{
AddressId = 5,
Box = "34",
City = "Antwerp",
Number = "1",
Street = "Italialei",
Zip = "2020"
};
context.Addresses.Add(address1);
context.Addresses.Add(address2);
context.Addresses.Add(address3);
context.Addresses.Add(address4);
context.Addresses.Add(address5);
}
}