ColumnAttribute.IsPrimaryKey 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定此類別成員代表的是表格主鍵的一部分或全部欄位。
public:
property bool IsPrimaryKey { bool get(); void set(bool value); };
public bool IsPrimaryKey { get; set; }
member this.IsPrimaryKey : bool with get, set
Public Property IsPrimaryKey As Boolean
屬性值
預設值 = false。
範例
[Column(Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ProductID
{
get
{
return this._ProductID;
}
set
{
if ((this._ProductID != value))
{
this.OnProductIDChanging(value);
this.SendPropertyChanging();
this._ProductID = value;
this.SendPropertyChanged("ProductID");
this.OnProductIDChanged();
}
}
}
<Column(Storage:="_ProductID", DbType:="Int NOT NULL", IsPrimaryKey:=true, IsDbGenerated:=true)> _
Public Property ProductID() As Integer
Get
Return Me._ProductID
End Get
Set
If ((Me._ProductID = value) _
= false) Then
If Me._Product.HasLoadedOrAssignedValue Then
Throw New System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException
End If
Me.OnProductIDChanging(value)
Me.SendPropertyChanging
Me._ProductID = value
Me.SendPropertyChanged("ProductID")
Me.OnProductIDChanged
End If
End Set
End Property
備註
假設是實體類別,你必須至少提供一個帶有此屬性的成員,且必須映射到主鍵或對應資料表或視圖中的唯一鍵。 若未達成此要求,LINQ TO SQL 將該類別實例視為唯讀,以提交資料庫變更。
若使用此特性指定類別中多個成員,則該鍵稱為相關欄位的複合體。
Note
LINQ 對 SQL 不支援計算欄位作為主鍵。