我在数据实体项目中使用 EF4,并且还有另一个包含业务实体的项目。
我正在使用显式运算符将数据实体对象转换为业务对象,第一个类是 Order,第二个是 SalesChannel
[DataContract]
public class Order
{
[DataMember]
public Guid OrderID { get; set; }
[DataMember]
public SalesChannel SalesChannel { get; set; }
public static explicit operator Order(Data.Entities.Order orderEntity)
{
return new Order()
{
OrderID = orderEntity.OrderID,
SalesChannel = **(SalesChannel)orderEntity.SalesChannel**
};
}
和
[DataContract]
public class SalesChannel
{
[DataMember]
public Guid SalesChannelID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Description { get; set; }
public static explicit operator SalesChannel(Data.Entities.SalesChannel salesChannelEntity)
{
return new SalesChannel()
{
SalesChannelID = salesChannelEntity.SalesChannelID,
Name = salesChannelEntity.Name,
Email = salesChannelEntity.Email,
Description = salesChannelEntity.Description
};
}
}
但是,在转换 SalesChannel 对象时,我收到此错误:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
我认为在 SalesChannel 类中没有正确地进行转换。
using (var orderDataProvider = new OrderDataProvider())
{
var orderList = orderDataProvider.GetAllOrders();
orders.AddRange(orderList.Select(order => (Order) order));
}