2011-01-01 4 views
1

Я просто пытаюсь узнать ADO.Net Data services Услуги передачи данных WCF.Проблема в ADO.Net Data Service при работе с Entity Model

Я только что создал образец программы, где у меня есть модель данных сущности базы данных Northwind, и я захватил продукты, категории и поставщиков в моей модели сущности.

Затем я добавил класс, который выглядит как

[DataServiceKey("ProductID")] 
public class ProductsService 
{ 
    public ProductsService() 
    { 
    } 

    public IQueryable<Product> Products 
    { 
     get 
     { 
     NORTHWNDEntities db = new NORTHWNDEntities(); 
     return db.Products.AsQueryable(); 
     } 
    } 
} 

Тогда у меня есть файл службы WCF Data, который выглядит как

public class ProductsDataService : DataService<ProductsService> 
{  
    public static void InitializeService(DataServiceConfiguration config) 
    {  
     config.SetEntitySetAccessRule("*", EntitySetRights.All);    
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; 
    } 
} 

Я знаю, что могу использовать NorthwindDataEntities класс, то он работает, но если я попытаюсь использовать класс ProductsService, у меня возникла проблема с ошибкой запроса ...

Любая идея, что здесь не так, пожалуйста ...

автоматически сгенерированный класс Продукта следующим образом, я добавил DataServiceKey атрибута ..

[EdmEntityTypeAttribute(NamespaceName="NORTHWNDModel", Name="Product")] 
[Serializable()] 
[DataContractAttribute(IsReference=true)] 
[DataServiceKey("ProductID")] 
public partial class Product : EntityObject 
{ 
     #region Factory Method 
     public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued) 
     { 
      Product product = new Product(); 
      product.ProductID = productID; 
      product.ProductName = productName; 
      product.Discontinued = discontinued; 
      return product; 
     } 

     #endregion 
     #region Primitive Properties 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
     [DataMemberAttribute()] 
     public global::System.Int32 ProductID 
     { 
      get 
      { 
       return _ProductID; 
      } 
      set 
      { 
       if (_ProductID != value) 
       { 
         OnProductIDChanging(value); 
         ReportPropertyChanging("ProductID"); 
         _ProductID = StructuralObject.SetValidValue(value); 
         ReportPropertyChanged("ProductID"); 
         OnProductIDChanged(); 
       } 
      } 
     } 
     private global::System.Int32 _ProductID; 
     partial void OnProductIDChanging(global::System.Int32 value); 
     partial void OnProductIDChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
     [DataMemberAttribute()] 
     public global::System.String ProductName 
     { 
      get 
      { 
       return _ProductName; 
      } 
      set 
      { 
       OnProductNameChanging(value); 
       ReportPropertyChanging("ProductName"); 
       _ProductName = StructuralObject.SetValidValue(value, false); 
       ReportPropertyChanged("ProductName"); 
       OnProductNameChanged(); 
      } 
     } 
     private global::System.String _ProductName; 
     partial void OnProductNameChanging(global::System.String value); 
     partial void OnProductNameChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public Nullable<global::System.Int32> SupplierID 
     { 
      get 
      { 
       return _SupplierID; 
      } 
      set 
      { 
       OnSupplierIDChanging(value); 
       ReportPropertyChanging("SupplierID"); 
       _SupplierID = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("SupplierID"); 
       OnSupplierIDChanged(); 
      } 
     } 
     private Nullable<global::System.Int32> _SupplierID; 
     partial void OnSupplierIDChanging(Nullable<global::System.Int32> value); 
     partial void OnSupplierIDChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public Nullable<global::System.Int32> CategoryID 
     { 
      get 
      { 
       return _CategoryID; 
      } 
      set 
      { 
       OnCategoryIDChanging(value); 
       ReportPropertyChanging("CategoryID"); 
       _CategoryID = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("CategoryID"); 
       OnCategoryIDChanged(); 
      } 
     } 
     private Nullable<global::System.Int32> _CategoryID; 
     partial void OnCategoryIDChanging(Nullable<global::System.Int32> value); 
     partial void OnCategoryIDChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public global::System.String QuantityPerUnit 
     { 
      get 
      { 
       return _QuantityPerUnit; 
      } 
      set 
      { 
       OnQuantityPerUnitChanging(value); 
       ReportPropertyChanging("QuantityPerUnit"); 
       _QuantityPerUnit = StructuralObject.SetValidValue(value, true); 
       ReportPropertyChanged("QuantityPerUnit"); 
       OnQuantityPerUnitChanged(); 
      } 
     } 
     private global::System.String _QuantityPerUnit; 
     partial void OnQuantityPerUnitChanging(global::System.String value); 
     partial void OnQuantityPerUnitChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public Nullable<global::System.Decimal> UnitPrice 
     { 
      get 
      { 
       return _UnitPrice; 
      } 
      set 
      { 
       OnUnitPriceChanging(value); 
       ReportPropertyChanging("UnitPrice"); 
       _UnitPrice = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("UnitPrice"); 
       OnUnitPriceChanged(); 
      } 
     } 
     private Nullable<global::System.Decimal> _UnitPrice; 
     partial void OnUnitPriceChanging(Nullable<global::System.Decimal> value); 
     partial void OnUnitPriceChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public Nullable<global::System.Int16> UnitsInStock 
     { 
      get 
      { 
       return _UnitsInStock; 
      } 
      set 
      { 
       OnUnitsInStockChanging(value); 
       ReportPropertyChanging("UnitsInStock"); 
       _UnitsInStock = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("UnitsInStock"); 
       OnUnitsInStockChanged(); 
      } 
     } 
     private Nullable<global::System.Int16> _UnitsInStock; 
     partial void OnUnitsInStockChanging(Nullable<global::System.Int16> value); 
     partial void OnUnitsInStockChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public Nullable<global::System.Int16> UnitsOnOrder 
     { 
      get 
      { 
       return _UnitsOnOrder; 
      } 
      set 
      { 
       OnUnitsOnOrderChanging(value); 
       ReportPropertyChanging("UnitsOnOrder"); 
       _UnitsOnOrder = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("UnitsOnOrder"); 
       OnUnitsOnOrderChanged(); 
      } 
     } 
     private Nullable<global::System.Int16> _UnitsOnOrder; 
     partial void OnUnitsOnOrderChanging(Nullable<global::System.Int16> value); 
     partial void OnUnitsOnOrderChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
     [DataMemberAttribute()] 
     public Nullable<global::System.Int16> ReorderLevel 
     { 
      get 
      { 
       return _ReorderLevel; 
      } 
      set 
      { 
       OnReorderLevelChanging(value); 
       ReportPropertyChanging("ReorderLevel"); 
       _ReorderLevel = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("ReorderLevel"); 
       OnReorderLevelChanged(); 
      } 
     } 
     private Nullable<global::System.Int16> _ReorderLevel; 
     partial void OnReorderLevelChanging(Nullable<global::System.Int16> value); 
     partial void OnReorderLevelChanged(); 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
     [DataMemberAttribute()] 
     public global::System.Boolean Discontinued 
     { 
      get 
      { 
       return _Discontinued; 
      } 
      set 
      { 
       OnDiscontinuedChanging(value); 
       ReportPropertyChanging("Discontinued"); 
       _Discontinued = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("Discontinued"); 
       OnDiscontinuedChanged(); 
      } 
     } 
     private global::System.Boolean _Discontinued; 
     partial void OnDiscontinuedChanging(global::System.Boolean value); 
     partial void OnDiscontinuedChanged(); 

     #endregion 

     #region Navigation Properties 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [XmlIgnoreAttribute()] 
     [SoapIgnoreAttribute()] 
     [DataMemberAttribute()] 
     [EdmRelationshipNavigationPropertyAttribute("NORTHWNDModel", "FK_Products_Categories", "Categories")] 
     public Category Category 
     { 
      get 
      { 
       return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("NORTHWNDModel.FK_Products_Categories", "Categories").Value; 
      } 
      set 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("NORTHWNDModel.FK_Products_Categories", "Categories").Value = value; 
      } 
     } 
     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [BrowsableAttribute(false)] 
     [DataMemberAttribute()] 
     public EntityReference<Category> CategoryReference 
     { 
      get 
      { 
       return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("NORTHWNDModel.FK_Products_Categories", "Categories"); 
      } 
      set 
      { 
       if ((value != null)) 
       { 
         ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Category>("NORTHWNDModel.FK_Products_Categories", "Categories", value); 
       } 
      } 
     } 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [XmlIgnoreAttribute()] 
     [SoapIgnoreAttribute()] 
     [DataMemberAttribute()] 
     [EdmRelationshipNavigationPropertyAttribute("NORTHWNDModel", "FK_Order_Details_Products", "Order_Details")] 
     public EntityCollection<Order_Detail> Order_Details 
     { 
      get 
      { 
       return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Order_Detail>("NORTHWNDModel.FK_Order_Details_Products", "Order_Details"); 
      } 
      set 
      { 
       if ((value != null)) 
       { 
         ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Order_Detail>("NORTHWNDModel.FK_Order_Details_Products", "Order_Details", value); 
       } 
      } 
     } 

     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [XmlIgnoreAttribute()] 
     [SoapIgnoreAttribute()] 
     [DataMemberAttribute()] 
     [EdmRelationshipNavigationPropertyAttribute("NORTHWNDModel", "FK_Products_Suppliers", "Suppliers")] 
     public Supplier Supplier 
     { 
      get 
      { 
       return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Supplier>("NORTHWNDModel.FK_Products_Suppliers", "Suppliers").Value; 
      } 
      set 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Supplier>("NORTHWNDModel.FK_Products_Suppliers", "Suppliers").Value = value; 
      } 
     } 
     /// <summary> 
     /// No Metadata Documentation available. 
     /// </summary> 
     [BrowsableAttribute(false)] 
     [DataMemberAttribute()] 
     public EntityReference<Supplier> SupplierReference 
     { 
      get 
      { 
       return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Supplier>("NORTHWNDModel.FK_Products_Suppliers", "Suppliers"); 
      } 
      set 
      { 
       if ((value != null)) 
       { 
         ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Supplier>("NORTHWNDModel.FK_Products_Suppliers", "Suppliers", value); 
       } 
      } 
     } 

     #endregion 
} 
+0

Вещь была переименована в ** WCF Data Services ** почти два года назад ... больше нет служб данных ADO.NET ... –

ответ

0

Вы не показали нам свой Product класса - нужен этот классу, чтобы иметь [DataServiceKey("ProductID")] на нем - для того, чтобы сделать это очистить DataService, как идентифицировать продукт:

[DataServiceKey("ProductID")] 
public partial class Product 
{ 
    public int ProductID { get; set; } 
    ... 
} 

С этим, я думаю, ваш подход должен работать.

Если нет: сообщите нам, что именно ошибка есть!

0

Без точной ошибки трудно догадаться, в чем проблема. Но в целом это не сработает. Причиной является различие в поведении IQueryable между LINQ to EF и LINQ to Objects. Если вы укажете ObjectContext (NorthwindEntities в вашем случае) в качестве контекста для службы данных, службы данных WCF предполагают поведение LINQ to EF и генерируют запросы таким образом, чтобы LINQ to EF мог его обрабатывать. Если вы укажете в качестве контекста для службы данных класс non-ObjectContext (ваш собственный в этом случае), он предполагает поведение LINQ to Objects и генерирует несколько разные запросы. Основное различие - это распространение в нуле (LINQ to Objects требует явного распространения null, тогда как LINQ to EF не нравится и обрабатывает его неявно), но есть и другие меньшие различия. Вам действительно нужно использовать свой собственный класс в качестве контекста, а не для класса, основанного на EF, на основе ObjectContext?

Смежные вопросы