如果我有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