1

I want to do the following in LINQ:

update [dbo].[AdminLogin] set [ADMIN_PASSWORD] = 'abc' where [ADMIN_ID] = 1

where i get the admin password from a model and the admin id is stored in a variable:

var userid = (from m in db.RequestPasswordReset
                   where m.Id == Unid
                   select m.UserId).FirstOrDefault();

How to do it?

Irshu
  • 8,248
  • 8
  • 53
  • 65
Sumedha Vangury
  • 643
  • 2
  • 17
  • 43
  • are you using EF context? – Karthik M R May 03 '16 at 10:01
  • Linq if for getting/filtering data, not for updating a database –  May 03 '16 at 10:01
  • @StephenMuecke I want to update the password of an existing user – Sumedha Vangury May 03 '16 at 10:02
  • Yes, but that's not linq is for. –  May 03 '16 at 10:04
  • @StephenMuecke ok so how do I do it – Sumedha Vangury May 03 '16 at 10:05
  • 1
    You get the object from the context (using a ling query), update its property, mark it as modified and call `db.SaveChanges()` - [Entity states and SaveChanges](https://msdn.microsoft.com/en-au/data/jj592676.aspx) –  May 03 '16 at 10:07
  • Hello, this is Clippy. It looks like you're trying to write your own login system without any knowledge about security. Are you sure you want to continue and put the safety of your users and your site in jeopardy? If not, please use ASP.NET Identity. – CodeCaster May 03 '16 at 10:10
  • @CodeCaster the system was made in such a way that I had to continue without using asp.net identity – Sumedha Vangury May 03 '16 at 10:13
  • Possible duplicate of [LINQ to Entities how to update a record](http://stackoverflow.com/questions/4617827/linq-to-entities-how-to-update-a-record) – CodeCaster May 03 '16 at 10:14

5 Answers5

4

To update an entity you must have to specify the Modified state.

using (var db= new DbContext())
{
  var entity= db.AdminLogin.Where(x => x.ADMIN_ID == userid ).SingleOrDefault();
  entity.ADMIN_PASSWORD = 'abc';
  db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
  db.SaveChanges();
}

see details here

Mahbubur Rahman
  • 4,961
  • 2
  • 39
  • 46
  • 2
    I believe you dont need to mark the entity state as modified as Ef will track it for you http://stackoverflow.com/questions/7106211/entity-framework-why-explicitly-set-entity-state-to-modified you could omit this line `db.Entry(entity).State = System.Data.Entity.EntityState.Modified; EF would do this for you` – Eldho May 03 '16 at 10:27
  • 1
    `Setting state manually is important in case of detached entities (entities loaded without change tracking or created outside of the current context)`. So using `Modified` means you are always in safe side. – Mahbubur Rahman May 03 '16 at 10:35
  • Yes its valid statement, But when you fetch the entity it will be tracked, Hence i believe if you save change by modifying it without marking it will still do a `update ` instead of insert . Correct me if i;m wrong – Eldho May 03 '16 at 10:40
1

use this code:

using (var context = new YourContext())
{
    var res = context.AdminLogin.Where(x => x.ADMIN_ID == userid ).SingleOrDefault();
    res.ADMIN_PASSWORD = 'abc';
    context.SaveChanges();
}
Karthik M R
  • 980
  • 1
  • 7
  • 12
1

You can try this:

var user = db.AdminLogin.FirstOrDefault(x => x.ADMIN_ID == id);
user.ADMIN_PASSWORD = 'abc';
db.SaveChanges();
Amine
  • 1,198
  • 11
  • 19
0
userid.ADMIN_PASSWORD= 'abc';
db.SaveChanges();  //better then db.SubmitChanges() in some case
S.Bozzoni
  • 998
  • 9
  • 18
0

If you using linQ.dbml file then you have to write like this:

using (var db= new DbContext())
{
  var entity= db.AdminLogin.Where(x => x.ADMIN_ID == userid ).FirstOrDefault();
  entity.ADMIN_PASSWORD = 'abc';

  db.SubmitChanges();
}
Kinjal Gohil
  • 958
  • 9
  • 15