表数据不可见的更改

时间:2015-03-20 14:00:25

标签: c# linq linq-to-sql transactions submitchanges

我想使用LINQ和SQL Server CE执行事务。

SubmitChanges方法似乎运行正常。但是,如果我查看数据表,则不会应用更改。

我的代码:

var query = (from s in this.tblRequirements
where s.abbrevation == "rml"
select new {s}).First();

System.Windows.Forms.MessageBox.Show("First: " + query.s.abbrevation);

query.s.abbrevation = "rmlas";

try
{ 
    this.rdb.SubmitChanges();
}
catch (ChangeConflictException e)
{ 
    System.Windows.Forms.MessageBox.Show(e.ToString()); 
}

System.Windows.Forms.MessageBox.Show("Second: " + query.s.abbrevation);

query = (from s in this.tblRequirements
         where s.requirementID == 4
         select new { s }).First();

System.Windows.Forms.MessageBox.Show("Third: " + query.s.abbrevation);

消息输出:

First = "rml"
Second = "rmlas"
Third = "rmlas"

我的表:

[requirementID] INT           IDENTITY (1, 1) NOT NULL,
[credentialID]  INT           DEFAULT (NULL) NULL,
[softwareName]  NCHAR(30)     NOT NULL,
[abbrevation]   NCHAR(10)     NOT NULL,
[uri]           NVARCHAR(MAX) DEFAULT (NULL) NULL,
[version]       NCHAR(15)     DEFAULT (NULL) NULL,
[installPath]   TEXT          DEFAULT (NULL) NULL,
[samedir]       BIT           DEFAULT ((0)) NULL,
[subPathID]     INT           NULL,

PRIMARY KEY CLUSTERED ([requirementID] ASC),
CONSTRAINT [FK_RequirementsAnnotation_Credentials] 
     FOREIGN KEY ([credentialID]) 
     REFERENCES [dbo].[Credential] ([credentialID]),
CONSTRAINT [FK_RequirementsAnnotation_SubPath] 
     FOREIGN KEY ([subPathID]) 
     REFERENCES [dbo].[SubPath] ([subPathID]) 

控制台输出:

> SELECT TOP (1) [t0].[requirementID], [t0].[credentialID],
> [t0].[softwareName], [t0].[abbrevation], [t0].[uri], [t0].[version],
> [t0].[installPath], [t0].[samedir], [t0].[subPathID] FROM
> [dbo].[RequirementsAnnotation] AS [t0] WHERE [t0].[abbrevation] = @p0
> -- @p0: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [rml]
> -- Context: SqlProvider(Sql2008) 
> Model: AttributedMetaModel Build: 4.0.30319.33440
> 
> UPDATE [dbo].[RequirementsAnnotation] SET [abbrevation] = @p7 WHERE
> ([requirementID] = @p0) AND ([credentialID] = @p1) AND ([softwareName]
> = @p2) AND ([abbrevation] = @p3) AND ([uri] = @p4) AND ([version] = @p5)
> AND ([installPath] IS NULL) AND (NOT ([samedir] = 1)) AND
> ([subPathID] = @p6)
> -- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]
> -- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
> -- @p2: Input NChar (Size = 30; Prec = 0; Scale = 0) [repeats    ]
> -- @p3: Input NChar (Size = 10; Prec = 0; Scale = 0) [rml       ]
> -- @p4: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [xyz]
> -- @p5: Input NChar (Size = 15; Prec = 0; Scale = 0) [20140131       ]
> -- @p6: Input Int (Size = -1; Prec = 0; Scale = 0) [2]
> -- @p7: Input NChar (Size = 10; Prec = 0; Scale = 0) [rmlas]
> -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel 

每次通话后都会显示此结果。

1 个答案:

答案 0 :(得分:0)

我使用此代码进行更新,您可以从中获得帮助。它对我有用。如果它不适合你,你有其他错误,而不是你发送的代码。

 using (phoneDBContext db1 = new phoneDBContext())
                {
                    IQueryable<Project> cityQuery = from c in db1.Projects
                                                    where c.Id == 56
                                                    select c;
                    Project p = cityQuery.FirstOrDefault();
                    p.Project_name = "rmlas";
                    db1.SubmitChanges();
                }