我有与班级帐户和城市相关联的班级联系人。城市与类状态关联,当我尝试插入新的联系人数据时,关联的类也被插入。这怎么会发生?以及如何解决?
方法 InsertOnSubmit
public void InsertOnSubmit<T>(T data) where T : class
{
lock (_lockObj)
{
using (DataModel dx = new DataModel(this._adapter.ConnectionString))
{
dx.DeferredLoadingEnabled = false;
dx.GetTable<T>().InsertOnSubmit(data);
dx.SubmitChanges();
}
}
}
添加新联系人
InsertOnSubmit(new Contact { Name = this.Name, Account = Session.Account, City = this.CurrentCity });
注意:对象城市、帐户和状态来自不同的数据上下文。我使用 SQL Server Compact 3.5 作为数据库服务器。
触发 dx.SubmitChanges() 时从控制台登录
INSERT INTO [State]([Name])
VALUES (@p0)
-- @p0: Input String (Size = 0; Prec = 0; Scale = 0) [Center America]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
SELECT CONVERT(Int,@@IDENTITY) AS [value]
-- @ROWCOUNT: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
INSERT INTO [City]([StateId], [Name])
VALUES (@p0, @p1)
-- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [10]
-- @p1: Input String (Size = 0; Prec = 0; Scale = 0) [Los Angeles]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
SELECT CONVERT(Int,@@IDENTITY) AS [value]
-- @ROWCOUNT: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
INSERT INTO [Contact]([AccountId], [CityId], [Name])
VALUES (@p0, @p1, @p2)
-- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input Int32 (Size = 0; Prec = 0; Scale = 0) [18]
-- @p2: Input String (Size = 0; Prec = 0; Scale = 0) [Sarah]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
SELECT CONVERT(Int,@@IDENTITY) AS [value]
-- @ROWCOUNT: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
问候,
布赖恩