2017-11-25 158 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. 
相关问题