4
  1. 我有一个与模型关联的车辆,模型与制造有关联。

  2. 这是我的通用存储库,与我的项目中的 LazyLoadingEnabled = false 关联有关:

    public IQueryable<T> IncludeMultiple<T1>(params Expression<Func<T, object>>[] associations) where T1 : class
    {
        var source = (IQueryable<T>)DbContext.Set<T>();
        if (associations != null)
        {
            foreach (Expression<Func<T, object>> path in associations)
                source = DbExtensions.Include<T, object>(source, path);
        }
        return source;
    }
    
  3. 在我的 api 控制器中,我使用的是工作单元模式。这是我的 GetAll 方法:

    public IEnumerable<Vehicle> GetAll()
    {
      var vehicles = Uow.VehicleRepository.IncludeMultiple<Vehicle>(c => c.VehicleModel).ToList();
      return vehicles;
    }
    

一切正常,Json 检索 Vehicle 类数据以及相关的 VehicleModel 类数据。

但是,Vehicle 与 VehicleMake 没有直接关联,只有 VehicleModel 有。现在,如果我的 GetAll 方法有这个:

    public IEnumerable<Vehicle> GetAll()
    {
      var vehicles = Uow.VehicleRepository.IncludeMultiple<Vehicle>(c => c.VehicleModel, c => c.VehicleModel.VehicleMake).ToList();
      return vehicles;
    }

虽然我在调试中看到车辆确实具有车辆及其相关的 VehicleModel 和 VehicleMake 数据,但它在 Fiddler 中返回一个 Http 500 错误。


更新:


在 Vehicle 中添加了另一个名为“Test”的关联,GetAll 方法为:

(c => c.VehicleModel, c => c.Test)

没有错误,所有数据都是通过提琴手返回的。因此,似乎“非直接关联”(即 Vehicle -> VehicleMake)是导致错误的原因。


问题:


检索相关车辆数据及其相关类的数据并将其返回给 Json 而不会出现 Http 500 错误的正确方法是什么?

4

1 回答 1

4

*已解决*


这有效:

  public HttpResponseMessage GetAll()
  {
      var vehicles = from data in  Uow.VehicleRepository.IncludeMultiple<Vehicle>(c => c.VehicleModel,c => c.VehicleModel.VehicleMake)
                         select new 
                             {
                                VehDesc = data.Description,
                                VehVIN = data.VIN,
                                VehTransmissionType = data.TransmissionType,
                                VehFuelType = data.FuelType,
                                VehYear = data.Year,
                                VehMileage = data.Mileage,
                                VehCylinderSize = data.CylinderSize,
                                VehEngineSize = data.EngineSize,
                                VehVehicleModel = data.VehicleModel.Name,
                                VehMakeName = data.VehicleModel.VehicleMake.Name
                             };
      return Request.CreateResponse(HttpStatusCode.OK, vehicles);
  }

基本上,

 1. I used an HttpResponseMessage as my return type;

 2. I used projection to create an anonymous type;

为什么我必须这样做?

据我所知,问题集中在 JSON 接收 VehicleModel 和 VehicleMake 的“循环”返回。也就是说,VehicleModel 有一个与 VehicleMake 的关联,而 VehicleMake 有一个 VehicleModel 的集合。当我查看调试代码时,我可以看到 VehicleModel 到 VehicleMake 到 VehicleModel 等的级联,等等,等等,所以对我来说这意味着它是循环的。

If anyone knows a better way w/o using anonymous type nor removing the virtual keyword from my navigation properties, I would certainly like to know it. But this does truly work.

FinalNote: Be sure NOT to use the model's property names in anonymous type, ie replace property "TransmissionType" with something like "VehTransmissionType".

于 2012-11-03T21:46:41.823 回答