0

I would like to perform a transaction by using LINQ and SQL Server CE.

The SubmitChanges method seems to work fine. But if I look in the data table the changes are not applied.

My code:

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);

Message output:

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

My table:

[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]) 

The console output:

> 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 

This result appears after each call.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ANKH
  • 3
  • 3
  • Which Database you check and not saw change in it ? Database that located in debug folder? or in main folder in your solution? – Rashed DIP Mar 20 '15 at 14:33
  • The database is RequirementsAnnotation and is located in DebugFolder under ./Databases (mdf, dbml) – ANKH Mar 20 '15 at 16:17
  • Are you using TransactionScope (without calling complete) anywhere in your program? Failing that, I would suspect that either you are looking at the wrong database, or your database is getting overwritten (possibly when you build your project). – sgmoore Mar 21 '15 at 12:08

1 Answers1

0

I using this code for update and you can help from it. it work well for me. if its not work for you, you have other wrong and not in that code you send.

 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();
                }
Rashed DIP
  • 57
  • 8