将数据库放在那里后从数据库中获取一个对象

时间:2014-06-29 10:17:27

标签: c# asp.net-mvc entity-framework

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

 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)

如何避免使用新数据上下文?

1 个答案:

答案 0 :(得分:0)

我在EF5中发现内存副本在保存到存储后没有更新,我最终使用了.Local

如此处in MSDN

所述