c# - 多表 CRUD 中的 WCF 错误

标签 c# wcf

我正在尝试为类别产品表上的 CRUD 操作创建一个 wcf 服务。 我添加了一个 linq to sql 文件并添加了两个表。

DBML 设计器代码(自动生成)

namespace WCTTESTMT {
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name = "MYDB")]
public partial class MYDBDataContext: System.Data.Linq.DataContext {
    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();#
    region Extensibility Method Definitions
    partial void OnCreated();
    partial void InsertCategory(Category instance);
    partial void UpdateCategory(Category instance);
    partial void DeleteCategory(Category instance);
    partial void InsertProduct(Product instance);
    partial void UpdateProduct(Product instance);
    partial void DeleteProduct(Product instance);#
    endregion
    public MYDBDataContext(): base(global::WCTTESTMT.Properties.Settings.Default.MYDBConnectionString, mappingSource) {
        OnCreated();
    }
    public MYDBDataContext(string connection): base(connection, mappingSource) {
        OnCreated();
    }
    public MYDBDataContext(System.Data.IDbConnection connection): base(connection, mappingSource) {
        OnCreated();
    }
    public MYDBDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource): base(connection, mappingSource) {
        OnCreated();
    }
    public MYDBDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource): base(connection, mappingSource) {
        OnCreated();
    }
    public System.Data.Linq.Table < Category > Categories {
        get {
            return this.GetTable < Category > ();
        }
    }
    public System.Data.Linq.Table < Product > Products {
        get {
            return this.GetTable < Product > ();
        }
    }
}
[global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.Categories")]
public partial class Category: INotifyPropertyChanging,
INotifyPropertyChanged {
    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
    private int _CategoryId;
    private string _CategoryTitle;
    private EntitySet < Product > _Products;#
    region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnCategoryIdChanging(int value);
    partial void OnCategoryIdChanged();
    partial void OnCategoryTitleChanging(string value);
    partial void OnCategoryTitleChanged();#
    endregion
    public Category() {
        this._Products = new EntitySet < Product > (new Action < Product > (this.attach_Products), new Action < Product > (this.detach_Products));
        OnCreated();
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_CategoryId", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int CategoryId {
        get {
            return this._CategoryId;
        }
        set {
            if ((this._CategoryId != value)) {
                this.OnCategoryIdChanging(value);
                this.SendPropertyChanging();
                this._CategoryId = value;
                this.SendPropertyChanged("CategoryId");
                this.OnCategoryIdChanged();
            }
        }
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_CategoryTitle", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
    public string CategoryTitle {
        get {
            return this._CategoryTitle;
        }
        set {
            if ((this._CategoryTitle != value)) {
                this.OnCategoryTitleChanging(value);
                this.SendPropertyChanging();
                this._CategoryTitle = value;
                this.SendPropertyChanged("CategoryTitle");
                this.OnCategoryTitleChanged();
            }
        }
    }
    [global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Category_Product", Storage = "_Products", ThisKey = "CategoryId", OtherKey = "CategoryId")]
    public EntitySet < Product > Products {
        get {
            return this._Products;
        }
        set {
            this._Products.Assign(value);
        }
    }
    public event PropertyChangingEventHandler PropertyChanging;
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void SendPropertyChanging() {
        if ((this.PropertyChanging != null)) {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }
    protected virtual void SendPropertyChanged(String propertyName) {
        if ((this.PropertyChanged != null)) {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private void attach_Products(Product entity) {
        this.SendPropertyChanging();
        entity.Category = this;
    }
    private void detach_Products(Product entity) {
        this.SendPropertyChanging();
        entity.Category = null;
    }
}
[global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.Products")]
public partial class Product: INotifyPropertyChanging,
INotifyPropertyChanged {
    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
    private int _ProductId;
    private int _CategoryId;
    private string _ProductTitle;
    private EntityRef < Category > _Category;#
    region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnProductIdChanging(int value);
    partial void OnProductIdChanged();
    partial void OnCategoryIdChanging(int value);
    partial void OnCategoryIdChanged();
    partial void OnProductTitleChanging(string value);
    partial void OnProductTitleChanged();#
    endregion
    public Product() {
        this._Category =
        default (EntityRef < Category > );
        OnCreated();
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_ProductId", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int ProductId {
        get {
            return this._ProductId;
        }
        set {
            if ((this._ProductId != value)) {
                this.OnProductIdChanging(value);
                this.SendPropertyChanging();
                this._ProductId = value;
                this.SendPropertyChanged("ProductId");
                this.OnProductIdChanged();
            }
        }
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_CategoryId", DbType = "Int NOT NULL")]
    public int CategoryId {
        get {
            return this._CategoryId;
        }
        set {
            if ((this._CategoryId != value)) {
                if (this._Category.HasLoadedOrAssignedValue) {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OnCategoryIdChanging(value);
                this.SendPropertyChanging();
                this._CategoryId = value;
                this.SendPropertyChanged("CategoryId");
                this.OnCategoryIdChanged();
            }
        }
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_ProductTitle", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
    public string ProductTitle {
        get {
            return this._ProductTitle;
        }
        set {
            if ((this._ProductTitle != value)) {
                this.OnProductTitleChanging(value);
                this.SendPropertyChanging();
                this._ProductTitle = value;
                this.SendPropertyChanged("ProductTitle");
                this.OnProductTitleChanged();
            }
        }
    }
    [global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Category_Product", Storage = "_Category", ThisKey = "CategoryId", OtherKey = "CategoryId", IsForeignKey = true)]
    public Category Category {
        get {
            return this._Category.Entity;
        }
        set {
            Category previousValue = this._Category.Entity;
            if (((previousValue != value) || (this._Category.HasLoadedOrAssignedValue == false))) {
                this.SendPropertyChanging();
                if ((previousValue != null)) {
                    this._Category.Entity = null;
                    previousValue.Products.Remove(this);
                }
                this._Category.Entity = value;
                if ((value != null)) {
                    value.Products.Add(this);
                    this._CategoryId = value.CategoryId;
                } else {
                    this._CategoryId =
                    default (int);
                }
                this.SendPropertyChanged("Category");
            }
        }
    }
    public event PropertyChangingEventHandler PropertyChanging;
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void SendPropertyChanging() {
        if ((this.PropertyChanging != null)) {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }
    protected virtual void SendPropertyChanged(String propertyName) {
        if ((this.PropertyChanged != null)) {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}}

这是产品类别的接口(interface)。

分类界面

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace WCTTESTMT
{
    [ServiceContract]
    public interface ICategory
    {

    [OperationContract]
    List<Category> getAllCategory();

    [OperationContract]
    Category getCategoryById(int id);

    [OperationContract]
    void AddCategory(Category oCategory);

    [OperationContract]
    void UpdateCategory(Category oCategory);


    [OperationContract]
    void DeleteCategory(int id);


    [OperationContract]
    List<Product> getProductByCategory(int id);

}}

产品界面

    using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace WCTTESTMT
{


    [ServiceContract]
    public interface IProduct
    {


[OperationContract]
    List<Product> getAllProduct();

    [OperationContract]
    Product getProductById(int id);

    [OperationContract]
    void AddProduct(Product oProduct);

    [OperationContract]
    void UpdateProduct(Product oProduct);


    [OperationContract]
    void DeleteProduct(int id);

}}

类别BL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WCTTESTMT
{
    public class CategoryBL : ICategory
    {

    MYDBDataContext octx = new MYDBDataContext();
    public List<Category> getAllCategory()
    {
        try
        {
            return octx.Categories.ToList();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public Category getCategoryById(int id)
    {
        try
        {
            return octx.Categories.Where(c => c.CategoryId == id).SingleOrDefault();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public void AddCategory(Category oCategory)
    {
        try
        {
            octx.Categories.InsertOnSubmit(oCategory);
            octx.SubmitChanges();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public void UpdateCategory(Category oCategory)
    {
        try
        {
            Category uCategory = getCategoryById(oCategory.CategoryId);
            uCategory.CategoryId = oCategory.CategoryId;
            uCategory.CategoryTitle = oCategory.CategoryTitle;
            octx.SubmitChanges();


        }
        catch (Exception)
        {

            throw;
        }
    }

    public void DeleteCategory(int id)
    {
        try
        {
            octx.Categories.DeleteOnSubmit(getCategoryById(id));
        }
        catch (Exception)
        {

            throw;
        }
    }

    public List<Product> getProductByCategory(int id)
    {
        try
        {
            return getCategoryById(id).Products.ToList();
        }
        catch (Exception)
        {

            throw;
        }
    }
}}

产品BL

using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

namespace WCTTESTMT
{


    public class ProductBL : IProduct
    {

    MYDBDataContext octx = new MYDBDataContext();
    public List<Product> getAllProduct()
    {
        try
        {
            return octx.Products.ToList();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public Product getProductById(int id)
    {
        try
        {
            return octx.Products.Where(c => c.ProductId == id).SingleOrDefault();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public void AddProduct(Product oProduct)
    {
        try
        {

            Category c =  new CategoryBL().getCategoryById(oProduct.CategoryId);

            Product uPro = new Product();
            uPro.ProductId = oProduct.ProductId;
            uPro.ProductTitle = oProduct.ProductTitle;
            uPro.CategoryId = c.CategoryId;


            octx.Products.InsertOnSubmit(uPro);
            octx.SubmitChanges();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public void UpdateProduct(Product oProduct)
    {
        try
        {
            Product uProduct = getProductById(oProduct.ProductId);
            uProduct.ProductId = oProduct.ProductId;
            uProduct.ProductTitle = oProduct.ProductTitle;
            uProduct.CategoryId = oProduct.CategoryId;
            octx.SubmitChanges();


        }
        catch (Exception)
        {

            throw;
        }
    }

    public void DeleteProduct(int id)
    {
        try
        {
            octx.Products.DeleteOnSubmit(getProductById(id));
        }
        catch (Exception)
        {

            throw;
        }
    }
}

App.Config 代码

<system.serviceModel>
<services>
  <service name="WCTTESTMT.CategoryBL">
    <endpoint address="wscat" binding="basicHttpBinding" name="wscat"
      contract="WCTTESTMT.ICategory">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mexcat" binding="mexHttpBinding" name="mexcat"
      contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8733/Design_Time_Addresses/WCTTESTMT/CategoryService/" />
      </baseAddresses>
    </host>
  </service>
  <service name="WCTTESTMT.ProductBL">
    <clear />
    <endpoint address="wspro" binding="basicHttpBinding" name="wspro"
      contract="WCTTESTMT.IProduct" listenUriMode="Explicit">
      <!--<identity>
        <certificateReference storeName="My" storeLocation="LocalMachine"
          x509FindType="FindBySubjectDistinguishedName" />
      </identity>-->
    </endpoint>
    <endpoint address="mexpro" binding="mexHttpBinding" bindingConfiguration=""
      name="mexpro" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8733/Design_Time_Addresses/WCTTESTMT/ProductService/" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, 
      set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true.  Set to false before deployment 
      to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>

DBML SHOWING RELATION OF TABLES

添加更新删除或获取类别没有问题。 添加产品时出现问题

Error While Add Product

最佳答案

好吧,我的一个 friend 建议在设计器代码中做这个改变

原创

[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Category_Product", Storage = "_Category", ThisKey = "CategoryId", OtherKey = "CategoryId", IsForeignKey = true)]
public Category Category {
    get {
        return this._Category.Entity;
    }
    set {
        Category previousValue = this._Category.Entity;
        if (((previousValue != value) || (this._Category.HasLoadedOrAssignedValue == false))) {
            this.SendPropertyChanging();
            if ((previousValue != null)) {
                this._Category.Entity = null;
                previousValue.Products.Remove(this);
            }
            this._Category.Entity = value;
            if ((value != null)) {
                value.Products.Add(this);
                this._CategoryId = value.CategoryId;
            } else {
                this._CategoryId =
                default (int);
            }
            this.SendPropertyChanged("Category");
        }
    }
}

更改public Category Category的访问修饰符 到内部类别类别

它现在工作正常,但它在概念上是否正确? .

关于c# - 多表 CRUD 中的 WCF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36641828/

相关文章:

c# - 如果经过特定时间,则在 C# 中显示消息框

c# - 响应 : 413 Request Entity Too Large

c# - WPF 应用程序引用 System.Windows.Forms

.net - 确定服务器上正在使用哪个WCF端点

c# - Easy WCF 程序,服务方法在客户端程序中不起作用

php - 使用 PHP 调用 WCF 服务(具有联合安全性)

.net - 如何使用多个参数调用 RESTful WCF 服务方法?

执行 File.Copy 时出现 C# unauthorizedAccessException

c# - 确定字符串是否在字符串数组中出现多次 (C#)

c# - 在与服务线程不同的线程上运行服务操作