2011-04-04 45 views
1

我需要Linq &可序列化,所以我使用autogenerate dbml並添加Seriable。我只需要「DVD表」可序列化,但有<如何在dbml中標記爲可序列化?

試了很多聯合收割機,還是失敗的錯誤:

Type 'System.Data.Linq.EntityRef`1[[CategoryList, App_Code.dpv5xabw, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' in Assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

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

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

private int _DvdId; 

private string _Title; 

private int _CategoryId; 

private EntityRef<CategoryList> _CategoryList; 

#region Extensibility Method Definitions 
partial void OnLoaded(); 
partial void OnValidate(System.Data.Linq.ChangeAction action); 
partial void OnCreated(); 
partial void OnDvdIdChanging(int value); 
partial void OnDvdIdChanged(); 
partial void OnTitleChanging(string value); 
partial void OnTitleChanged(); 
partial void OnCategoryIdChanging(int value); 
partial void OnCategoryIdChanged(); 
#endregion 

public DvdList() 
{ 
    this._CategoryList = default(EntityRef<CategoryList>); 
    OnCreated(); 
} 

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

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Title", DbType="VarChar(50) NOT NULL", CanBeNull=false)] 
public string Title 
{ 
    get 
    { 
     return this._Title; 
    } 
    set 
    { 
     if ((this._Title != value)) 
     { 
      this.OnTitleChanging(value); 
      this.SendPropertyChanging(); 
      this._Title = value; 
      this.SendPropertyChanged("Title"); 
      this.OnTitleChanged(); 
     } 
    } 
} 

    [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._CategoryList.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.AssociationAttribute(Name="CategoryList_DvdList", Storage="_CategoryList", ThisKey="CategoryId", OtherKey="CategoryId", IsForeignKey=true, DeleteOnNull=true, DeleteRule="CASCADE")] 
public CategoryList CategoryList 
{ 
    get 
    { 
     return this._CategoryList.Entity; 
    } 
    set 
    { 
     CategoryList previousValue = this._CategoryList.Entity; 
     if (((previousValue != value) 
        || (this._CategoryList.HasLoadedOrAssignedValue == false))) 
     { 
      this.SendPropertyChanging(); 
      if ((previousValue != null)) 
      { 
       this._CategoryList.Entity = null; 
       previousValue.DvdLists.Remove(this); 
      } 
      this._CategoryList.Entity = value; 
      if ((value != null)) 
      { 
       value.DvdLists.Add(this); 
       this._CategoryId = value.CategoryId; 
      } 
      else 
      { 
       this._CategoryId = default(int); 
      } 
      this.SendPropertyChanged("CategoryList"); 
     } 
    } 
} 

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

}

回答

4

您可以在定義它部分班。

創建另一個.cs文件,然後使用:

[Serializable] 
public partial class DvdList 
{ 

} 
+1

受審。問題中顯示錯誤。問題是在linq中,這個類中有entryRef導致這個Serializable問題。我確實需要Serialzable。 – jenifer 2011-04-04 00:10:41

+0

@jenifer - 您還需要將所有複雜類型屬性類標記爲可序列化。 – jfar 2011-04-04 13:02:17

+0

如何將*內建* EntityRef類型標記爲'[Serializable]'? – 2011-05-26 15:58:40

相關問題