2016-04-15 8 views
2

カテゴリ製品テーブルでcrud操作用のwcfサービスを作成しようとしています。 SQLファイルにlinqを追加して、両方のテーブルを追加しました。複数テーブルでのWCFエラーCRUD

DBMLデザイナーCODE(自動生成)

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)); 
     } 
    } 
}} 

これは、製品カテゴリーのインタフェースです。

カテゴリーインタフェース

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); 

}} 

CategoryBL

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; 
     } 
    } 
}} 

ProductBL

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

答えて

1

オーケーA友達に

オリジナルデザイナーのコードでこの変更を行うことが示唆発生

[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"); 
     } 
    } 
} 

変更公衆カテゴリーカテゴリー のアクセス修飾子内部カテゴリカテゴリ

それは概念的に正しいのですか? 。

関連する問題