Reputation: 7626
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?
Upvotes: 0
Views: 1588
Reputation: 26765
You need to map to an existing entity:
var entity = dbContext.TblUsers.Find<TblUser>(dto.Id);
Mapper.Map<UserDTO, TblUser>(dto, entity);
dbContext.SaveChanges();
Upvotes: 1