我将模型对象保存在数据库中
var orderModel = new Order
{
Id = Guid.NewGuid(),
LastName = lastName,
FirstName = firstName,
MiddleName = middleName,
Country = country,
ZipCode = zipcode,
City = city,
Street = street,
HomeLocation = homeLocation,
Email = email,
Phone = phone,
Comment = comment,
ItemId = orderedItem.Id,
UserId = _dataManager.Users.GetUserById(new Guid("20ed0117-8747-44c5-b24b-95412661f403")).Id,
CompanyId = _dataManager.Companys.GetCompanyById(ConfigurationInformation.GetCompanyId()).Id,
CreateDateTime = DateTime.Now,
Price = price,
Quantity = quantity
};
_dataManager.Orders.SaveOrder(orderModel); // Save order
然后得到这个对象:
var order = _dataManager.Orders.GetOrderById(orderModel.Id);
用户,公司和项目为空
我的模型看起来像这样:
public class Order
{
[Key]
public System.Guid Id { get; set; }
public Nullable<double> Price { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required]
public string Country { get; set; }
[Required]
public int ZipCode { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Street { get; set; }
[Required]
public string HomeLocation { get; set; }
[Required]
public string Email { get; set; }
public string Phone { get; set; }
public string Comment { get; set; }
public int CompanyId { get; set; }
[Required]
public System.DateTime CreateDateTime { get; set; }
public System.Guid UserId { get; set; }
public Nullable<System.Guid> ItemId { get; set; }
public virtual Company Company { get; set; }
public virtual Item Item { get; set; }
public virtual User User { get; set; }
}
如果我使用新的数据上下文得到这个对象,那么一切都会好的:
var order = (new BussinesLogic.DataDirections.QuillingShopDataContext()).Orders.FirstOrDefault(x => x.Id == orderModel.Id)
如何避免使用新数据上下文?