1

I am trying to update a column in my database using linq. Following is my code which I am using to update the user id. I also tried with updating the name. Still, I am not able to update the record.

DMS_GenericDataContext db = new DMS_GenericDataContext();

Document_Master doc1 = db.Document_Masters
  .Where(c => c.Document_Id_Prefix == "PRD_TST_TST1_T2_" && c.Document_Id == "2")
  .Single();

doc1.User_IDD = "anuragnigam";
db.SubmitChanges();
jnovo
  • 5,659
  • 2
  • 38
  • 56
user3004443
  • 133
  • 1
  • 1
  • 16
  • @ChristosPaisios : no. I did not get any error. After the line db.SubmitChanges, if i take curor to doc1 variable, I can see the chaged User_IDD but the change is not reflecting in the database. I am using MS SQL 2005. Does this has any relation with the record not being updated.? – user3004443 Feb 10 '14 at 10:22

3 Answers3

1

Try

db.SaveChanges();

That would save all changes made in this context to the underlying database. Here's a full thread that would help you deal with updates : Update Entity using DBContext

Update : seems that your version is fine enough, your entity/table must have a primary key a primary key helps identify a table and differentiate its instances from on another

AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84
  • I am getting an error for line SaveChanges. the error is DMS_Generic doe not contain a method as SaveChanges. – user3004443 Feb 10 '14 at 10:27
  • I am new to LINQ. How can I check the version on EF? I am using .net framework 4.0. Is the upation not happening because of the absence of primary key.? the table Document_Master has no primary key. – user3004443 Feb 10 '14 at 10:34
  • @user3004443 Since your table has no primary key, how can you identify each row of your table? A primary key is the identity we usually use for differentiate one record in a table from the others records in the same table. – Christos Feb 10 '14 at 10:37
1
using(DMS_GenericDataContext db = new DMS_GenericDataContext())
{

    Document_Master doc1 = db.Document_Masters
                             .Where(c => c.Document_Id_Prefix == "PRD_TST_TST1_T2_" &&                  c.Document_Id == "2")
                             .SingleOrDefault();

    // If the record exists, then make the corresponding update. 
    if(doc1!=null)
    {
       doc1.User_IDD = "anuragnigam";
       db.SaveChanges();
    }
}

Try to use the using statement, when you use disposable objects like a DataContext. Also, you should use SingleOrDefault() rather than use Single(), because if you use the latter and there isn't any record like the one you are looking, you will get an exception. On the other hand, if you use SingleOrDefault(), you will not get an exception. If there isn't the record in the db, you will get a null object. Last but not least, you should check if the Document_Master isn't null. If it isn't, that mean that there exist the record in your db and you can make the corresponding update.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • I am getting an error for line SaveChanges. the error is DMS_Generic doe not contain a method as SaveChanges. – user3004443 Feb 10 '14 at 10:26
  • I am new to LINQ. How can I check the version on EF? I am using .net framework 4.0 – user3004443 Feb 10 '14 at 10:32
  • Just look at your references folder. There should be a reference to Entity Framework. If you right click on it and then select properties, the properties widnow will appear. Then look at Version. – Christos Feb 10 '14 at 10:34
  • I guess it is version 5.0 – user3004443 Feb 10 '14 at 10:37
  • Then you should use the solution provided by Sameer, the reason why I am saying this is this one http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record. – Christos Feb 10 '14 at 10:40
1

To update an existing but disconnected object, you need to attach it do the data context. 'Attach' has an optional bool parameter to treat as modified.

doc1.User_IDD = "anuragnigam";
db.Document_Masters.Attach(doc1);
db.SubmitChanges();
Sameer
  • 2,143
  • 1
  • 16
  • 22
  • I am new to LINQ. How can I check the version on EF? I am using .net framework 4.0. Is the upation not happening because of the absence of primary key.? the table Document_Master has no primary key. I got an error at the db.Document_Masters.Attach(doc1) – user3004443 Feb 10 '14 at 10:35
  • If you don't have primary key to `Table` then you can't use Entity Framework. Add Primary key to your table. For Finding `Entity Framework` version go to project references select "EntityFramework.dll" and check `Version` in `Properties`. – Sameer Feb 10 '14 at 10:38
  • Done. Thanks man.. But now tell me I have composite keys as you can see Document_Id_Prefix and Document_Id. How can I update this table using LINQ – user3004443 Feb 10 '14 at 10:46
  • If you have mapping issue check http://stackoverflow.com/questions/2733254/how-do-i-map-a-composite-primary-key-in-entity-framework-4-code-first and [Handle Composite Keys in Queries](http://msdn.microsoft.com/en-us/library/bb399391.aspx) – Sameer Feb 10 '14 at 10:56