1

When I update this entity I get this error:

My Class for database is different with my class in viewmodel

but I convert it.

Please help me and send me the correct code.

Thanks

My error:

Attaching an entity of type 'DomainModel.Models.Tbl_Images' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate

My code:

public bool Update(ImagesEditVM model)
{
        bool result = false;

        try
        {
            DomainModel.Models.Tbl_Images img = new Tbl_Images
            {
                Id = model.Id,
                Code = model.Code,
                Image = model.Image,
                Language = model.Language,
                Title = model.Title
            };

            db.Tbl_Images.Attach(img);
            db.Entry<DomainModel.Models.Tbl_Images>(img).State = EntityState.Modified;
            db.SaveChanges();      

            result = true;
            return result;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
}

2 Answers 2

1

One way is to query the image first. Then update the values and explicitly call SaveChanges or SaveChangesAsync.

try
{
    var updatingImage = _db.Tbl_Images.FirstOrDefault(i => i.Id == model.Id);

    if (updatingImage != null)
    {
        // either manually map those values or use auto mapper.
        updatingImage.Code = model.Code;
        updatingImage.Image = model.Image;
        updatingImage.Language = model.Language;
        updatingImage.Title = model.Title;

        _db.Tbl_Images.Update(updatingImage);
        _db.SaveChanges();
    }
    ...
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I Changed db.Tbl_Images.Update(updatingImage); to db.Tbl_Images.Attach(updatingImage); db.Entry<DomainModel.Models.Tbl_Images>(updatingImage).State = EntityState.Modified; Work it .THanks
1

you can try with this:

    public bool Update ( ImagesEditVM model )
    {
        bool result = false;
        try
        {

            var existing = db.Tbl_Images.Find ( model.Id );
            if ( existing == null )
            {
                context.Add ( model );
            }
            else
            {
                db.Entry ( existing ).CurrentValues.SetValues ( model);
            }

            context.SaveChanges ( );


            result = true;
            return result;
        }
        catch ( Exception ex )
        {
            throw new Exception ( ex.Message );
        }
    }

see this Link

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.