如果我有LINQ對象:獲取對象的DataContext的
public class SampleDataContext : DataContext {
public Table<Customer> Customers { get { return this.GetTable<Customer>(); } }
public SampleDataContext(string connectionString) : base(connectionString) { }
}
[Table(Name="dbo.tblCustomers")]
public class Customer {
private Guid? customerID;
[Column(Storage="customerID", DbType="uniqueidentifier NOT NULL", IsPrimaryKey=true)]
public Guid? CustomerID {
get { return this.customerID; }
set { this.customerID = value; }
}
private string customerName;
[Column(Storage = "customerName", DbType = "nvarchar(255) NOT NULL")]
public string CustomerName {
get { return this.customerName; }
set { this.customerName = value; }
}
}
應用
和別的地方:
public static void DoSomethingWithCustomer(Customer customer) {
// some operations
// now, I want save changes to the database
}
我怎麼能得到的DataContext的實例跟蹤的「客戶」對象的變化?
編輯:爲什麼我不想將DataContext傳入方法。
1)傳遞總是2個對象而不是1是整個應用程序的「醜陋」模式。
- 方法將需要每個業務對象的下一個參數。
- 集合將需要從「列表」更改爲「列表>」。
這兩點將會更加難以維持 - 開發商必須的每次設置的DataContext的正確實例(很容易地創建一個bug),儘管在DataContext知道具體的對象(或沒有)連接到其他的DataContext 。
2)我希望(當前版本的應用程序使用它)處理來自不同「地點」(浮動窗口,例如拖放&)的對象集合的任何業務邏輯。
Currentyl我們使用自定義類型的DataSets,因此有關更改的信息位於數據行(DataRow =業務對象)中,並不是獲取它的問題,或者創建一個克隆然後將其保存到數據庫中。
重新給我你的意見,你根本不應該使用這種方法:這意味着數據上下文是* *還長(因爲兩者隱式鏈接),但數據上下文通常應被視爲短期資源。 – 2008-11-10 06:56:36
我修改了我們的需求和linq的功能,看起來linq-to-sql不是正確的方法,我們應該去。也許linq-to-entities可以提供所需的功能。 謝謝你的答案。 – TcKs 2008-11-10 11:04:19