【问题标题】:how to mark as serializable in dbml?如何在 dbml 中标记为可序列化?
【发布时间】:2011-04-04 00:00:50
【问题描述】:

我需要 Linq 和 Serializable,所以我使用自动生成 dbml 并添加 Seriable。我只需要可序列化的“DVD 表”,但是有

尝试了很多组合,仍然因为错误而失败:

在程序集“System.Data.Linq, Version=4.0”中键入“System.Data.Linq.EntityRef`1[[CategoryList, App_Code.dpv5xabw, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]] .0.0、Culture=neutral、PublicKeyToken=b77a5c561934e089' 未标记为可序列化。

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

}

【问题讨论】:

    标签: c# linq-to-sql serialization


    【解决方案1】:

    您可以在部分类中定义它。

    创建另一个 .cs 文件,然后使用:

    [Serializable]
    public partial class DvdList
    {
    
    }
    

    【讨论】:

    • 以前试过。问题中显示了错误。问题是在linq中,这个类DvdList中有entryRef导致了这个Serializable问题。我确实需要 Serialzable。
    • @jenifer - 您还需要将所有复杂类型的属性类标记为可序列化。
    • 如何将内置 EntityRef 类型标记为[Serializable]
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2015-12-26
    • 2011-12-06
    • 2017-02-21
    • 2015-11-10
    • 2016-01-18
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多