0

Is there a way to use SetValues() when creating an entity framework object.

I have a class with same field definitions as my entity framework class and found this explanation of an easier way to update ef records using the SetValues function.

https://researchaholic.com/2013/02/06/entity-framework-5-easier-way-to-update-record/

The question I have is this: Is there a similar way to create new ef records without specifying every field like this:

var newItm = new Import.EntityClasses.ImportSiteList {
    SiteId = ssItm.SiteId,
    OrganizationId = ssItm.OrganizationId,
    TimeZoneId = ssItm.TimeZoneId
};
tbl.Add(newItm);

1 Answer 1

1

This is how you can do

var newItm = new Import.EntityClasses.ImportSiteList();
context.Entry(newItm).State = EntityState.Added;
context.Entry(newItm).CurrentValues.SetValues(ssItm);
context.SaveChanges();

Hope it helps.

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.