1

This is my table ts_grp_perm_mapping

grp_permid     grp_id      perm_id
22              4           1
23              2           2

This is my code to delete row. usr_groupId below is grp_id in above table. usr_doctypeids below is perm_id in above table.

public int saveNewUser(int usr_groupId, int usr_doctypeids)
{
     ts_grp_perm_mapping tm = db.ts_grp_perm_mapping.Find(usr_groupId);
     db.ts_grp_perm_mapping.Remove(tm);
     int rowiseffected=db.SaveChanges();
     return rowiseffected;
}

When I trace my tm shows null, and error pops up like values cannot be null. So where I am going wrong?

3
  • Please give actual definition of ts_grp_perm_mapping. Commented Feb 3, 2016 at 9:13
  • grp_id is primary key? Commented Feb 3, 2016 at 9:16
  • grp_id is forein key. grp_permid is primary. I am trying to delete based on grp_id. Commented Feb 3, 2016 at 10:00

3 Answers 3

2

If grp_id is Primary Key

 public int saveNewUser(int usr_groupId, int usr_doctypeids)
    {
         ts_grp_perm_mapping tm = db.ts_grp_perm_mapping.SingleOrDefault(ts => ts.grp_id == usr_groupId);
         if(tm != null)
             {
         db.ts_grp_perm_mapping.Remove(tm);
         int rowiseffected=db.SaveChanges();
         return rowiseffected;
    }
    else
    {
    return 0;
    }
    }

If grp_id is not a primary key

public int saveNewUser(int usr_groupId, int usr_doctypeids)
        {
             ts_grp_perm_mapping tm = db.ts_grp_perm_mapping.FirstOrDefault(ts => ts.grp_id == usr_groupId);
             if(tm != null)
                 {
             db.ts_grp_perm_mapping.Remove(tm);
             int rowiseffected=db.SaveChanges();
             return rowiseffected;
        }
        else
        {
        return 0;
        }
        }
Sign up to request clarification or add additional context in comments.

Comments

1

Find only works for the Primary Key. Use Linq Single or SingleOrDefault for other properties.

public int saveNewUser(int usr_groupId, int usr_doctypeids)
{
     ts_grp_perm_mapping tm = db.ts_grp_perm_mapping.SingleOrDefault(ts => ts.grp_id == usr_groupId);
     if(tm == null)
         throw new Exception($"The grp_id {usr_groupId} was not found");
     db.ts_grp_perm_mapping.Remove(tm);
     int rowiseffected=db.SaveChanges();
     return rowiseffected;
}

And if possible make your method async and use

ts_grp_perm_mapping tm = await db.ts_grp_perm_mapping.SingleOrDefaultAsync(ts => ts.grp_id == usr_groupId).ConfigureAwait(false);
//...
int rowiseffected = await db.SaveChangesAsync().ConfigureAwait(false);

2 Comments

The above method worked for me. Thanks, Also can i know the usage if savechangesasync()?
If you don't know how async and await work, i suggest you read the MSDN article at msdn.microsoft.com/en-us/library/…
-1

Try

        public int saveNewUser(int usr_groupId, int usr_doctypeids) 
    { 
      var tm = db.ts_grp_perm_mapping.Where(a => a.grp_id == usr_groupId); 
      db.ts_grp_perm_mapping.Remove(tm); 
      int rowiseffected=db.SaveChanges();
      return rowiseffected;
    }

4 Comments

why should OP try this? Could you elaborate more on your answer?
This won't work, Where returns IQueryable<ts_grp_perm_mapping> and Remove expects ts_grp_perm_mapping you need to use RemoveRange
.Find is looking for the primary key. grp_id isn't the primary key, but OP is feeding in the grp_id
Thanks for everyone whoever helped me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.