0

I am using Automapper with Telerik Open Access ORM. Now my problem is there are 5 columns in my table tblUser like Firstname, Lastname, Username, Email and Password. Now when admin creates new user, it's password generated by some logic.

The problem is in update. Currently I show password field on user detail page with readonly textbox. Now I don't want to show password field. So is there anyway I skip password field while update?

I tried following but it throw error that password field as null.

var map = AutoMapper.Mapper.CreateMap<UserDTO, TblUser>().ForSourceMember(x=>x.Password,y=>y.Ignore());
this.Update(entity);
this.Save();
return entity;

public void Update(T entity)
{
  dbContext.AttachCopy<U>(AutoMapper.Mapper.Map<U>(entity));
}

Error:

Cannot insert the value NULL into column 'Password', table 'MyDatabase.dbo.tblUsers'; column does not allow nulls. UPDATE fails.\r\nThe statement has been terminated.

I don't know how to update by skipping some fields.

For suppose I have 10 columns in insertion form and 5 columns in updation form for particular table, So while update I am taking other 5 column as hidden field to avoid error. But now I don't want to take other 5 fields as hidden. Isn't there any alternative exist?

6
  • Pass previous password while updating Commented Sep 10, 2014 at 6:41
  • @Justcode I don't want to pass it. So I need an alternative. For suppose I have 10 columns and form allows updation of only 5 columns, I am taking other 5 column as hidden field to avoid error. But now I don't want to take other 5 fields as hidden. Isn't there any alternative exist? Commented Sep 10, 2014 at 6:42
  • column does not allow nulls so you dont want to insert in that table ? Commented Sep 10, 2014 at 6:46
  • @SagarPilkhwal I want to insert, but at time of update i want to update only that fields which is updated. not others like password. Commented Sep 10, 2014 at 6:46
  • 2
    You haven't shown your Save() method, but you can ignore a property(s) when saving as shown in this answer Commented Sep 10, 2014 at 8:32

1 Answer 1

1

You need to map to an existing entity:

var entity = dbContext.TblUsers.Find<TblUser>(dto.Id);
Mapper.Map<UserDTO, TblUser>(dto, entity);
dbContext.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

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.