Salesforce connect - can't add new record

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
mccartp
Posts: 2
Joined: Thu 24 Oct 2019 02:23

Salesforce connect - can't add new record

Post by mccartp » Thu 24 Oct 2019 02:50

Dear All

I've installed the ADO.NET Provider for Salesforce in Visual Studio 2017 and 2019.

I can connect to my developer salesforce org no problems in server explorer. I can also create create an ADO.NET Entity Data Model using EF5 no problems and connect to it in a console application and read contents of tables.

I can't however add new records in my c# console application;

var db = new Entities1();
var contacts = db.Contacts;
var model = new Contact();
model.FirstName = "Bill";
model.LastName = "Bloggs";
model.Name = "Bill Bloggs";
model.OwnerId = "xxxxxxx";
model.Gender = "Male";
contacts.Add(model);
db.SaveChanges();

The code fails on the final line with a entity validation error of "The Id field is required.". Happens in both VS 2017 and 2019.

I understand this field to be the primary key, ready only and I guess it would normally be created within Salesforce itself so its unclear to me how to handle this.

I did create an edml instead of the edmx to see if that offered a solution using EF6. But my code can't open it - fails with a message about the username, password or token being incorrect even though they are not because they were used to create the edml successfully in the first place.

I'm really keen to find a solution to this since my ability to successfully add data would mean that I can port our entire database to Salesforce (200+ tables) which is the objective.

Thanks, Phil

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Salesforce connect - can't add new record

Post by Shalex » Sat 26 Oct 2019 14:28

mccartp wrote: Thu 24 Oct 2019 02:50The code fails on the final line with a entity validation error of "The Id field is required.". Happens in both VS 2017 and 2019.

I understand this field to be the primary key, ready only and I guess it would normally be created within Salesforce itself so its unclear to me how to handle this.
You should set StoreGeneratedPattern=Identity for the Id property in SSDL (Store) part of your model: https://docs.microsoft.com/en-us/dotnet ... mework-4.8.

EDM Wizard/Designer (*.edmx) allows editing only CSDL part of the model in design time. You should open *.edmx with XML Editor and set StoreGeneratedPattern=Identity in SSDL.

We recommend you to use Entity Developer (Devart Entity Model, *.edml) instead of EDM Designer (ADO.NET Entity Data Model, *.edmx) because it is adjusted for working with Salesforce and has an advanced functionality: http://www.devart.com/entitydeveloper/ed-vs-edm.html. Additionally, Entity Developer adds registration of EF6-provider in app.config automatically and allows editing SSDL in design time.
mccartp wrote: Thu 24 Oct 2019 02:50I did create an edml instead of the edmx to see if that offered a solution using EF6. But my code can't open it - fails with a message about the username, password or token being incorrect even though they are not because they were used to create the edml successfully in the first place.
Right-click on your *.edml in Solution Explorer > Open With > Entity Developer. This should open *.edml in Entity Developer embedded into Visual Studio. Navigate to Tools > Entity Developer > Database Explorer, open Connection Editor, set Persist Security Info=True. Does this help?

mccartp
Posts: 2
Joined: Thu 24 Oct 2019 02:23

Re: Salesforce connect - can't add new record

Post by mccartp » Wed 30 Oct 2019 01:37

Dear Shalex

Thanks for your response. I tried the EDMX first but found that for my custom object the StoreGeneratedPattern was already set as Identity.

So I abandoned that and revisited the EDML and found that Persist Security Info was indeed set as False. I changed this to True and my code was then able to save the record successfully - phew.

Regards, Phil.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Salesforce connect - can't add new record

Post by Shalex » Wed 30 Oct 2019 20:16

mccartp wrote: Wed 30 Oct 2019 01:37I tried the EDMX first but found that for my custom object the StoreGeneratedPattern was already set as Identity.
Make sure that StoreGeneratedPattern=Identity is set in SSDL part of your model, it is not enough to set it in CSDL.
mccartp wrote: Wed 30 Oct 2019 01:37So I abandoned that and revisited the EDML and found that Persist Security Info was indeed set as False. I changed this to True and my code was then able to save the record successfully - phew.
Thank you for letting us know.

Post Reply