我正在使用视图模型将客户、账单和运输详细信息获取到我的控制器中,并尝试保存数据。由于最初未设置 customerid,因此我收到与外键相关的错误。我无法理解这其中出了什么问题。
public class Customer
{
public Customer()
{
BillingAddress = new BillingAddress();
}
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public BillingAddress BillingAddress { get; set; }
}
public class BillingAddress
{
public long Id { get; set; }
public long CustomerId { get; set; }
public string Address1 { get; set; }
public string City { get; set; }
public Customer Customer { get; set; }
}
public class CustomerController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public CustomerController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
[HttpPost]
public IActionResult SaveCustomerDetails(CustomerViewModel viewModel)
{
if (!ModelState.IsValid)
return View(viewModel);
Customer customer = new Customer
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName
};
_unitOfWork.Customers.Add(customer);
BillingAddress billingAddress = new BillingAddress
{
Address1 = viewModel.BillingAddress.Address1,
City = viewModel.BillingAddress.City,
State = viewModel.BillingAddress.State
CustomerId = customer.Id
};
_unitOfWork.BillingAddress.Add(billingAddress);
_unitOfWork.SaveChanges();
return View("Index");
}
}
您只需要在调用保存更改之前构建对象图 - EF 将处理它。
这比调用保存更改以获取 ID 然后保存地址要好得多。 这种方式将是单个数据库命中并且是单个事务(即全部成功或全部失败(,而不是中间保存,如果其中一个失败,可能会使数据库处于意外状态:
public IActionResult SaveCustomerDetails(CustomerViewModel viewModel)
{
if (!ModelState.IsValid)
return View(viewModel);
Customer customer = new Customer
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
BillingAddress = new BillingAddress
{
Address1 = viewModel.BillingAddress.Address1,
City = viewModel.BillingAddress.City,
State = viewModel.BillingAddress.State
}
};
_unitOfWork.Customers.Add(customer);
_unitOfWork.SaveChanges();
return View("Index");
}
这是一个工作演示,如下所示:
型:
public class Customer
{
public Customer()
{
BillingAddress = new BillingAddress();
}
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public BillingAddress BillingAddress { get; set; }
}
public class BillingAddress
{
public long Id { get; set; }
public long CustomerId { get; set; }
public string Address1 { get; set; }
public string City { get; set; }
public Customer Customer { get; set; }
}
public class CustomerViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public BillingAddress BillingAddress { get; set; }
}
控制器:
public class CustomersController : Controller
{
private readonly MvcProjContext _context;
public CustomersController(MvcProjContext context)
{
_context = context;
}
[HttpPost]
public IActionResult Create(CustomerViewModel viewModel)
{
if (!ModelState.IsValid)
return View(viewModel);
Customer customer = new Customer
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName
};
_context.Customer.Add(customer);
_context.SaveChanges();//add this line
//find the added customer id
var CustomerId = _context.Customer
.Where(c => c.FirstName == viewModel.FirstName && c.LastName == viewModel.LastName)
.Select(c => c.Id).FirstOrDefault();
BillingAddress billingAddress = new BillingAddress
{
Address1 = viewModel.BillingAddress.Address1,
City = viewModel.BillingAddress.City,
CustomerId = CustomerId
};
_context.BillingAddress.Add(billingAddress);
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
}