2017-11-25 264 views
-1

我將創建一個模塊,用於使用MVC C#將產品添加到期望列表中。爲此,我使用了實體框架。我在模型文件夾中創建了dbml類。我在去增加產品至我的喜愛我得到的錯誤是這樣的:獲取錯誤「表<WishList>」不包含'添加'的定義,也沒有包含edmx linq的擴展方法'使用mvc c#添加'?

「‘表’不包含‘添加’,並沒有 擴展方法‘添加’接受的第一argumnets一個認定中鍵入 「表」可以找到。(是否缺少using指令 或程序集引用?)

我不知道爲什麼這個錯誤在這裏產生的,什麼是缺少在這裏我的代碼中的任何好友那麼請讓我知道。下面我列出了我的代碼。

這是我的dbml類表確定指標:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.WishList")] 
public partial class WishList : INotifyPropertyChanging, INotifyPropertyChanged 
{ 

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

    private int _Id; 

    private string _Laminate_Sheet_Smo; 

    private System.Nullable<int> _CustmorId; 

    public List<WishList> list { get; set; } 

    #region Extensibility Method Definitions 
    partial void OnLoaded(); 
partial void OnValidate(System.Data.Linq.ChangeAction action); 
partial void OnCreated(); 
partial void OnIdChanging(int value); 
partial void OnIdChanged(); 
partial void OnLaminate_Sheet_SmoChanging(string value); 
partial void OnLaminate_Sheet_SmoChanged(); 
partial void OnCustmorIdChanging(System.Nullable<int> value); 
partial void OnCustmorIdChanged(); 
#endregion 

    public WishList() 
    { 
     OnCreated(); 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] 
    public int Id 
    { 
     get 
     { 
      return this._Id; 
     } 
     set 
     { 
      if ((this._Id != value)) 
      { 
       this.OnIdChanging(value); 
       this.SendPropertyChanging(); 
       this._Id = value; 
       this.SendPropertyChanged("Id"); 
       this.OnIdChanged(); 
      } 
     } 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Laminate_Sheet_Smo", DbType="NVarChar(50)")] 
    public string Laminate_Sheet_Smo 
    { 
     get 
     { 
      return this._Laminate_Sheet_Smo; 
     } 
     set 
     { 
      if ((this._Laminate_Sheet_Smo != value)) 
      { 
       this.OnLaminate_Sheet_SmoChanging(value); 
       this.SendPropertyChanging(); 
       this._Laminate_Sheet_Smo = value; 
       this.SendPropertyChanged("Laminate_Sheet_Smo"); 
       this.OnLaminate_Sheet_SmoChanged(); 
      } 
     } 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CustmorId", DbType="Int")] 
    public System.Nullable<int> CustmorId 
    { 
     get 
     { 
      return this._CustmorId; 
     } 
     set 
     { 
      if ((this._CustmorId != value)) 
      { 
       this.OnCustmorIdChanging(value); 
       this.SendPropertyChanging(); 
       this._CustmorId = value; 
       this.SendPropertyChanged("CustmorId"); 
       this.OnCustmorIdChanged(); 
      } 
     } 
    } 

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

這是一流的,我有使自定義的也是我的模型文件夾:

public class WishListCs 
{ 
    [Key] 
    public int id { get; set; } 

    public string Laminate_Sheet_Smo { get; set; } 

    public int CustmorId { get; set; } 
} 

這是我的控制器代碼:

DataClasses1DataContext db = new DataClasses1DataContext(); 
if (!string.IsNullOrEmpty(CookieHelper.GetCookieValue(CookieKeys.WishLaminateId))) 
      { 

       string pids = CookieHelper.GetCookieValue(CookieKeys.WishLaminateId); 
       var v = pids.Split(','); 
       var length = v.Length; 

       for (int i = 0; i < length; i++) 
       { 
        WishList addtbl = new WishList(); 
        addtbl.Laminate_Sheet_Smo = v[i]; 
        addtbl.CustmorId = Convert.ToInt32(CookieHelper.GetCookieValue(CookieKeys.CustId)); 
        db.WishLists.Add() // here i am getting error 
        int UserId = Convert.ToInt32(CookieHelper.GetCookieValue(CookieKeys.CustId)); 
        var h = db.WishLists.Where(x => x.CustmorId == UserId).Count(); 
        CookieHelper.SetCookie(CookieKeys.CountOfCart, h.ToString()); 
       } 
      } 

這是我的代碼請任何一位幫助我如何解決這個錯誤。

+0

@大衛布朗你有想法如何解決這個錯誤? – coder

+2

我認爲它應該是'db.WishLists.Add(addtbl)'而不是'db.WishLists.Add()' –

回答

0

試試這個:

WishList addtbl = new WishList(); 
addtbl.Laminate_Sheet_Smo = v[i]; 
addtbl.CustmorId = Convert.ToInt32(CookieHelper.GetCookieValue(CookieKeys.CustId)); 
db.WishLists.Add(addtbl); //Add the object into the DBSet 
db.SaveChanges(); //Save changes to database. 
相關問題