从数据库中获取一个对象,然后把它放在那里

本文关键字:在那里 然后 数据库 获取 一个对象 | 更新日期: 2023-09-27 18:10:45

我将模型对象保存在数据库中

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

User, Company和Item为空

我的模型是这样的:

 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)

我如何得到这个不使用新的数据上下文?

从数据库中获取一个对象,然后把它放在那里

我发现在EF5中的内存副本保存到存储后没有更新,我最终使用。local

在MSDN

中解释
相关文章: