光脚丫学LINQ(026):如何使实体可序列化

视频演示:http://u.115.com/file/f274c53ada

光脚重点总结
要想使对象模型中的实体类对象可被序列化,就应该在实体类的定义中使用DataContractAttribute属性修饰,以及在实体类的列成员中使用DataMemberAttribute属性修饰就可以了。比如下面的代码:

[Table(Name="dbo.Customers")]   
[DataContract()]   
public partial class Customers   
{    
 [Column(Storage="_CustomerID")]   
 [DataMember(Order=1)]   
 public string CustomerID   
 {   
  get{ return this._CustomerID; }   
  set{ this._CustomerID = value; }   
 }   
}  
[Table(Name="dbo.Customers")]
[DataContract()]
public partial class Customers
{ 
 [Column(Storage="_CustomerID")]
 [DataMember(Order=1)]
 public string CustomerID
 {
  get{ return this._CustomerID; }
  set{ this._CustomerID = value; }
 }
}


上面的这个实体类对象就可以被序列化。
要向对实体类做这样的修改,除了使用手动的方式以外,还可以借助SQLMetal和对象关系设计器这样工具。
可以使用如下的SQLMetal命令来创建可被序列化的实体类。

SqlMetal /code:"C:\Northwind.cs" /language:csharp "C:\LINQ\Northwind.mdf" /serialization:unidirectional


上面这个命令决定实体类对象是否可序列化的参数是 /serialization:unidirectional
至于对象关系设计器,则可以设置【序列化模式】设置为单向来实现这点。

原文地址:https://www.cnblogs.com/GJYSK/p/1866331.html