I find that there are two ways at least to create a record in an entity like the following.
Common Part
var record = new someEntity()
{
attribute1="test1",
attribute2="test2"
};
var service = new OrganizationService("CrmConnectionString");
Part A
service.Create(record);
Part B
var orgContext = new OrganizationServiceContext(service);
orgContext.AddObject(record);
orgContext.SaveChanges();
What is the difference?And which is better?
Part A uses the raw create method of the organization service proxy. This operation directly creates the record.
Part B makes use of the OrganizationServiceContext, which implements the Unit of Work pattern. Your operations are not transmitted to the server until you call SaveChanges()
Which is better? It depends on your requirements. If you only want to create a record on the go -> use the service. If you do multiple things which form a logical unit take version B.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With