在 LINQ to SQL 中,以開發人員程式設計語言表示的物件模型會對應至關係資料庫的數據模型。 接著會根據物件模型對數據進行作業。
在此案例中,您不會對資料庫發出資料庫命令(例如 INSERT
, )。 相反地,您會變更值,並在物件模型中執行方法。 當您想要查詢資料庫或傳送變更時,LINQ to SQL 會將要求轉譯成正確的 SQL 命令,並將這些命令傳送至資料庫。
下表摘要說明 LINQ to SQL 物件模型中最基本的元素,以及它們與關係型數據模型中元素的關聯性:
LINQ to SQL 物件模型 | 關係型數據模型 |
---|---|
實體類別 | 表 |
類別成員 | 資料行 |
協會 | 外鍵關聯性 |
方法 | 預存程式或函數 |
備註
下列描述假設您具備關係型數據模型和規則的基本知識。
LINQ to SQL 實體類別和資料庫數據表
在 LINQ to SQL 中,資料庫數據表是由 實體類別表示。 實體類別就像您可能建立的任何其他類別,不同之處在於您使用將類別與資料庫數據表建立關聯的特殊資訊來標註類別。 您可以藉由將自訂屬性 (TableAttribute) 新增至類別宣告來進行此批注,如下列範例所示:
範例
[Table(Name = "Customers")]
public class Customerzz
{
public string CustomerID;
// ...
public string City;
}
<Table(Name:="Customers")> _
Public Class Customer
Public CustomerID As String
' ...
Public City As String
End Class
只有宣告為數據表的類別實例(也就是實體類別)可以儲存至資料庫。
如需詳細資訊,請參閱 Attribute-Based 映射的數據表屬性一節。
LINQ to SQL 類別成員和資料庫數據行
除了將類別與數據表產生關聯之外,您還可以指定欄位或屬性來代表資料庫數據行。 為了達到此目的,LINQ to SQL 會 ColumnAttribute 定義 屬性,如下列範例所示:
範例
[Table(Name = "Customers")]
public class Customer
{
[Column(IsPrimaryKey = true)]
public string CustomerID;
[Column]
public string City;
}
<Table(Name:="Customers")> _
Public Class Customer
<Column(IsPrimaryKey:=True)> _
Public CustomerID As String
<Column()> _
Public City As String
End Class
只有對應至數據行的欄位和屬性會保存至資料庫或從資料庫擷取。 未宣告為欄位的部分將被視為應用程式邏輯的暫時性部分。
屬性 ColumnAttribute 有各種不同的屬性,可用來自定義這些代表數據行的成員(例如,將成員指定為代表主鍵數據行)。 如需詳細資訊,請參閱Attribute-Based 對應的欄屬性部分。
LINQ to SQL 關聯及資料庫外鍵關係
在 LINQ to SQL 中,您可以套用 AssociationAttribute 屬性來代表資料庫關聯性(例如外鍵與主鍵關聯性)。 在下列程式碼區段中,類別 Order
包含一個具有 Customer
屬性的 AssociationAttribute 屬性。 這個屬性及其特徵提供了 Order
類別和 Customer
類別之間的關聯。
下列程式代碼範例顯示 Customer
類別中的 Order
屬性。
範例
[Association(Name="FK_Orders_Customers", Storage="_Customer", ThisKey="CustomerID", IsForeignKey=true)]
public Customer Customer
{
get
{
return this._Customer.Entity;
}
set
{
Customer previousValue = this._Customer.Entity;
if (((previousValue != value)
|| (this._Customer.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Customer.Entity = null;
previousValue.Orders.Remove(this);
}
this._Customer.Entity = value;
if ((value != null))
{
value.Orders.Add(this);
this._CustomerID = value.CustomerID;
}
else
{
this._CustomerID = default(string);
}
this.SendPropertyChanged("Customer");
}
}
}
<Association(Name:="FK_Orders_Customers", Storage:="_Customer", ThisKey:="CustomerID", IsForeignKey:=true)> _
Public Property Customer() As Customer
Get
Return Me._Customer.Entity
End Get
Set
Dim previousValue As Customer = Me._Customer.Entity
If (((previousValue Is value) _
= false) _
OrElse (Me._Customer.HasLoadedOrAssignedValue = false)) Then
Me.SendPropertyChanging
If ((previousValue Is Nothing) _
= false) Then
Me._Customer.Entity = Nothing
previousValue.Orders.Remove(Me)
End If
Me._Customer.Entity = value
If ((value Is Nothing) _
= false) Then
value.Orders.Add(Me)
Me._CustomerID = value.CustomerID
Else
Me._CustomerID = CType(Nothing, String)
End If
Me.SendPropertyChanged("Customer")
End If
End Set
End Property
如需詳細資訊,請參閱 Attribute-Based 對應的關聯屬性一節。
LINQ to SQL 方法和資料庫預存程式
LINQ to SQL 支援預存程式和使用者定義函式。 在 LINQ to SQL 中,您可以將這些資料庫定義的抽象概念對應至客戶端物件,讓您可以從用戶端程式代碼以強型別的方式存取它們。 方法簽章與資料庫中定義之程式和函式的簽章非常類似。 您可以使用 IntelliSense 來探索這些方法。
呼叫對應程式所傳回的結果集是強型別集合。
LINQ to SQL 會使用 FunctionAttribute 和 ParameterAttribute 屬性,將預存程式和函式對應至方法。 表示預存程式的方法與 使用者定義函數的方法由 IsComposable 屬性區分。 如果此屬性設定為 false
(預設值),則 方法代表預存程式。 如果設定為 true
,則方法表示資料庫函式。
備註
如果您使用 Visual Studio,您可以使用物件關係型設計工具來建立對應至預存程式和使用者定義函式的方法。
範例
// This is an example of a stored procedure in the Northwind
// sample database. The IsComposable property defaults to false.
[Function(Name="dbo.CustOrderHist")]
public ISingleResult<CustOrderHistResult> CustOrderHist([Parameter(Name="CustomerID", DbType="NChar(5)")] string customerID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
return ((ISingleResult<CustOrderHistResult>)(result.ReturnValue));
}
' This is an example of a stored procedure in the Northwind
' sample database. The IsComposable property defaults to false.
<FunctionAttribute(Name:="dbo.CustOrderHist")> _
Public Function CustOrderHist(<Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal customerID As String) As ISingleResult(Of CustOrderHistResult)
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), customerID)
Return CType(result.ReturnValue, ISingleResult(Of CustOrderHistResult))
End Function
如需詳細資訊,請參閱 Attribute-Based 對應 和 預存程式的函數屬性、預存程式屬性和參數屬性一節。