【问题标题】:Problem in ADO.Net Data Service when working with Entity Model使用实体模型时 ADO.Net 数据服务中的问题
【发布时间】:2011-01-04 21:17:14
【问题描述】:

我只是想学习ADO.Net 数据服务WCF 数据服务。

我刚刚创建了一个示例程序,其中我有 Northwind 数据库的实体数据模型,并且我已经在我的实体模型中抓取了 Products、Category 和 Supplier 实体。

然后我添加了一个看起来像

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

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

然后我有一个 WCF 数据服务文件,看起来像

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

我知道我可以使用 NorthwindDataEntities 类然后它可以工作,但如果我尝试使用我的 ProductsService 类,我会收到请求错误问题...

请知道这里出了什么问题...

自动生成的Product类如下,我添加了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
 }   

【问题讨论】:

  • 这个东西在大约两年前被重命名为 WCF 数据服务...不再有 ADO.NET 数据服务...

标签: c# wcf-data-services


【解决方案1】:

您没有向我们展示您的 Product 类 - 该类需要有 [DataServiceKey("ProductID")] - 以便让 DataService 清楚如何识别产品:

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

有了这个,我认为你的方法应该有效。

如果不是:请告诉我们确切错误是什么!

【讨论】:

    【解决方案2】:

    如果没有确切的错误,很难猜测实际问题是什么。但总的来说,这不起作用。原因是 LINQ to EF 和 LINQ to Objects 之间的 IQueryable 行为不同。如果您将 ObjectContext(在您的情况下为 NorthwindEntities)指定为数据服务的上下文,WCF 数据服务将假定 LINQ to EF 行为并以 LINQ to EF 可以处理的方式生成查询。如果您指定一个非 ObjectContext 类(在这种情况下是您自己的)作为数据服务的上下文,它会假定 LINQ to Objects 行为并生成一些不同的查询。主要区别在于空传播(LINQ to Objects 需要显式空传播,而 LINQ to EF 不喜欢它并隐式处理它),但还有其他较小的差异。 您真的需要使用自己的类作为上下文而不是 EF 生成的基于 ObjectContext 的类吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多