How to force the Plugin on Post-Operation to Submit? - plugins

I have a plugin in post-operation witch need to create a folder on sharepoint via webservice, to do that, my plugin calls a webservice to execute a FechXML to get the info from the entity, but the problem is that entity still not exist, and it give me Null.
How do i force the plugin to submit/save the data to my FechXml to work?
PLUGIN CODE:
try
{
Entity entity;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "fcg_processos")
{
throw new InvalidPluginExecutionException("Ocorreu um erro no PlugIn Create Folder.");
}
}
else
{
throw new InvalidPluginExecutionException("Ocorreu um erro no PlugIn Create Folder.");
}
processosid = (Guid)((Entity)context.InputParameters["Target"])["fcg_processosid"];
string processoid2 = processosid.ToString();
PluginSharepointProcessos.ServiceReference.PrxActivityResult result = log.CreateFolderSP("Processo", processoid2);
string resultado = result.xmlContent;
if (result.retCode > 0)
{
throw new InvalidPluginExecutionException("Ocorreu um erro na criação do Folder do Processo.");
}
WEBSERVICE CODE:
{
//WEBSERVICE TO CALL XML FROM ENTITY
PrxActivityResult Processo = ProcessoFetch2("", "", guid);
string stxml;
stxml = Processo.XmlContent;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stxml);
XmlNodeList nodeList = xmlDoc.SelectNodes("resultset/result");
List<string[]> lista = new List<string[]>();
string[] strs = new string[7];
if (nodeList.Count != 0)//verificar o numero de registos
{
foreach (XmlNode xmlnode in nodeList)
{
if (xmlnode.SelectSingleNode("//fcg_numero") != null)
strs[2] = xmlnode.SelectSingleNode("//fcg_numero").InnerText;
else
strs[2] = "";
if (xmlnode.SelectSingleNode("//Concurso.fcg_numero") != null)
strs[3] = xmlnode.SelectSingleNode("//Concurso.fcg_numero").InnerText;
else
strs[3] = "";
}
}
IwsspClient FmwSharepoint = new IwsspClient();
PrxActivityResult folderresult = new PrxActivityResult();
List<ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave> arrayfields = new List<ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave>();
ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave nprocesso = new ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave();
nprocesso.Key = "FCG_Numero_Processo";
nprocesso.value = strs[2];
arrayfields.Add(nprocesso);
ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave npconcurso = new ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave();
npconcurso.Key = "FCG_Numero_Concurso";
npconcurso.value = strs[3];
arrayfields.Add(npconcurso);
ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave npguid = new ws.fcg.sipp.svc.ServiceReferenceSharePoint.PareChave();
npguid.Key = "FCG_Guid_CRM";
npguid.value = guid;
arrayfields.Add(npguid);
folderresult = FmwSharepoint.CreateFolder("http://localhost/folder", "Processos", strs[2], arrayfields.ToArray());
res = folderresult;
}

When a plugin runs on the Post-Operation, it is still within the database transaction, and it hasn't actually been committed to the database. Any calls done with the service reference passed in as a part of the Plugin Context will be executed within the context on the database transaction and you will be able to retrieve the newly created/updated values. If you create a brand new OrganizationServiceProxy (Which I'm guessing is what you're doing), it will execute outside of the database transaction, and will not see the newly created / updated values.
As #AndyMeyers suggests in his comment (which really should be an answer IMHO), grabbing the data from the plugin context either via a pre/post image or the target is ideal since it eliminates another database call. If you're having to lookup records that may have been created by another plugin that fired earlier, you'll need to use the IOrganizationService that is included in the plugin context.

I had no option and I used this code to run webservice based on image and forget the FecthXml, as mentioned, i get all info from the Image on the post operation and send back to the WebService. Thanks, here is the code:
entity = (Entity)context.InputParameters["Target"];
concursid = (Guid)entity.Attributes["fcg_concursid"];
guid = concursid.ToString();
string npconcurs = (string)entity.Attributes["fcg_numer"];
nconcurs= npconcurs;
EntityReference nprograma = (EntityReference)entity.Attributes["fcg_unidadeorganica"];
program = nprogram.Name;
if (entity.LogicalName != "fcg_concurs")

Related

Custom properties are not updated for the word via openXML

I am trying to update custom properties of word document thru Open XML programming but it seems the updated properties are not getting saved properly for the word document. So when I opening document after successful execution of the update custom property code, I am getting the message box which is "This document contains field that may refer to other files; Do you want to update the fields in the Document?" If I am pressing 'NO' button then all the update properties would not be saved to the document. If we are going for yes option then it will update properties but I need to save the properties explicitly. Please suggest to save properties to the document without getting confirmation message or corrupting the document. :)
the code snippet is given as below,
public void SetCustomValue(
WordprocessingDocument document, string propname, string aValue)
{
CustomFilePropertiesPart oDocCustomProps = document.CustomFilePropertiesPart;
Properties props = oDocCustomProps.Properties;
if (props != null)
{
//logger.Debug("props is not null");
foreach (var prop in props.Elements<CustomDocumentProperty>())
{
if (prop != null && prop.Name == propname)
{
//logger.Debug("Setting Property: " + prop.Name + " to value: " + aValue);
prop.Remove();
var newProp = new CustomDocumentProperty();
newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";
newProp.Name = prop.Name;
VTLPWSTR vTLPWSTR1 = new VTLPWSTR();
vTLPWSTR1.Text = aValue;
newProp.Append(vTLPWSTR1);
props.AppendChild(newProp);
props.Save();
}
}
int pid = 2;
foreach (CustomDocumentProperty item in props)
{
item.PropertyId = pid++;
}
props.Save();
}
}
I am using .Net framework 3.5 with Open XML SDK 2.0 and Office 2013.
Try this one
var CustomeProperties = xmlDOc.CustomFilePropertiesPart.Properties;
foreach (CustomDocumentProperty customeProperty in CustomeProperties)
{
if (customeProperty.Name == "DocumentName")
{
customeProperty.VTLPWSTR = new VTLPWSTR("My Custom Name");
}
else if (customeProperty.Name == "DocumentID")
{
customeProperty.VTLPWSTR = new VTLPWSTR("FNP.SMS.IQC");
}
else if (customeProperty.Name == "DocumentLastUpdate")
{
customeProperty.VTLPWSTR = new VTLPWSTR(DateTime.Now.ToShortDateString());
}
}
//Open Word Setting File
DocumentSettingsPart settingsPart = xmlDOc.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
//Update Fields
UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen();
updateFields.Val = new OnOffValue(true);
settingsPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields);
settingsPart.Settings.Save();
you have to update your document fields on open.

Entities may have been modified or deleted since entities were loaded. Error during transaction

During the execution of the following piece of code, I get the message
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
And there are loads of occurrences of that in here, but I didn't find a solution to my problem.
This only happens to me because I'm running _db.SaveChanges() two times inside one transaction (_db is my database context object), and I HAVE to do so because I need the generated ID that it gives me in order to proceed with the code.
If anyone can help me, I'd appreciate it. Also, if you know a way to proceed without the two _db.SaveChanges() or with a different way of approaching the transaction, I welcome you to show it.
using (var trans = _db.Database.BeginTransaction())
{
try
{
var f = cbxFornecedor.SelectedItem as Fornecedor;
var c = new Compra
{
CompraId = compra.CompraId,
DataCompra = dtpDataCompra.Value,
ListaProdutos = new List<ListaProdutos>(),
Fornecedor = f,
Referencia = tbxReferencia.Text,
Situacao = rbtEntregue.Checked
};
_db.Compras.Add(c);
_db.SaveChanges();
foreach (var cada in _itens)
c.ListaProdutos.Add(new ListaProdutos
{
Compra = c,
CompraId = c.CompraId,
Produto = cada.ProdutoClasse,
ProdutoId = cada.ProdutoClasse.ProdutoId,
Valor = cada.ValorTotal,
Quantidade = cada.Quantidade,
});
foreach (var cada in c.ListaProdutos)
if (_db.ListaProdutos.Find(cada.CompraId, cada.ProdutoId) != null)
_db.Entry(cada).State = EntityState.Modified;
else
_db.ListaProdutos.Add(cada);
_db.Entry(c).State = EntityState.Modified;
_db.SaveChanges();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
MessageBox.Show(this, ex.Message, #"Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
And, just to be clear, I know that my problem is with that fact that the transaction is there. I tried removing it and it worked. Also, if I take out the second _db.SaveChanges(), it works too.
The exception is only thrown in the second _db.SaveChanges(), and only if it is surrounded by the transaction block.
It is not direct answer to your question, but it may be helpful.
You do not need to specify Compra and CompraId for new ListaProdutos because EF smart enough to figure it out. Also it looks like that you do not need _db.ListaProdutos.Find(cada.CompraId, cada.ProdutoId) != null check because cada.CompraId - is new Id and it can not be in DB at this moment. Also you do not need _db.ListaProdutos.Add(cada); because EF already new that you are adding this records. Can you try next code?
var f = cbxFornecedor.SelectedItem as Fornecedor;
var c = new Compra
{
DataCompra = dtpDataCompra.Value,
ListaProdutos = new List<ListaProdutos>(),
Fornecedor = f,
Referencia = tbxReferencia.Text,
Situacao = rbtEntregue.Checked
};
_db.Compras.Add(c);
foreach (var cada in _itens)
c.ListaProdutos.Add(new ListaProdutos
{
Produto = cada.ProdutoClasse,
ProdutoId = cada.ProdutoClasse.ProdutoId,
Valor = cada.ValorTotal,
Quantidade = cada.Quantidade,
});
_db.SaveChanges();

Error "The given key was not present in the dictionary" When Creating New Custom Entity on Update Event

On CRM 2013 on-premise, I'm trying to write a plugin that triggers when an update is made to a field on Quote. The plugin then creates a new custom entity "new_contract".
My plugin is successfully triggered when the update to that field is made. However I keep getting an error message "The given key was not present in the dictionary" when trying to create the new custom entity.
I'm using a "PostImage" in this code. I confirm that it's registered using the same name in Plugin Registration.
Here is the code
var targetEntity = context.GetParameterCollection<Entity>
(context.InputParameters, "Target");
if (targetEntity == null)
{throw new InvalidPluginExecutionException(OperationStatus.Failed,
"Target Entity cannot be null")}
var postImage = context.PostEntityImages["PostImage"];
if (postImage == null)
{throw new InvalidPluginExecutionException(OperationStatus.Failed,
"Post Image is required");}
var quote = context.GenerateCompositeEntity(targetEntity, postImage);
//throw new InvalidPluginExecutionException(OperationStatus.Failed, "Update is captured");
//Guid QuoteId = (Guid)quote.Attributes["quoteid"];
var serviceFactory = (IOrganizationServiceFactory)serviceProvider
.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
var contractEntity = new Entity();
contractEntity = new Entity("new_contract");
if (quote.Attributes.Contains("portfolio"))
{
var quotePortfolio = (EntityReference)quote.Attributes["new_portfolio];
contractEntity[Schema.new_contract.PortfolioName] =
new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
}
if (quote.Attributes.Contains(Schema.Quote.QuoteName))
{
var quoteName = (string)quote.Attributes["name"];
contractEntity[Schema.new_contract.contractName] = quoteName;
}
var contractId = service.Create(contractEntity);
I think context does not contain "PostImage" attribute.You should check context to see whether it contains the attribute before getting the data.
Looking at this line in your post above:
var service = serviceFactory.CreateOrganizationService(context.UserId);
I am deducing that the type of your context variable is LocalPluginContext (since this contains the UserId value) which does not expose the images (as another answer states).
To access the images, you need to get to the Plugin Execution Context:
IPluginExecutionContext pluginContext = context.PluginExecutionContext;
Entity postImage = null;
if (pluginContext.PostEntityImages != null && pluginContext.PostEntityImages.Contains("PostImage))
{
postImage = pluginContext.PostEntityImages["PostImage"];
}
In the below code segment, you are checking for the attribute "portfolio" and using "new_portfolio". Can you correct that and let us know whether that worked.
if (quote.Attributes.Contains("portfolio"))
{
var quotePortfolio = (EntityReference)quote.Attributes["new_portfolio];
contractEntity[Schema.new_contract.PortfolioName] = new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
}
First, you don't say what line is throwing the exception. Put in the VS debugger and find the line that is throwing the exception.
I did see that you are trying to read from a dictionary here without first checking if the dictionary contains the key, that can be the source of this exception.
var postImage = context.PostEntityImages["PostImage"];
if (postImage == null)
throw new InvalidPluginExecutionException(OperationStatus.Failed,
"Post Image is required");
Try this:
if(!context.PostEntityImages.Contains("PostImage") ||
context.PostEntityImages["PostImage"] == null)
InvalidPluginExecutionException(OperationStatus.Failed, "Post Image is required");
var postImage = context.PostEntityImages["PostImage"];
Although, I don't think that a PostEntityImage Value will ever be null, if it passes the Contains test you don't really need the null check.

Cannot create/update a Rally TestCaseResult with a reference to a specified TestSet

With Rally, I need to
Update a TestCaseResult with a new TestSet Ref.
OR
Create a new TestCaseResult by copying everything from a previous TestCaseResult and just changing the TestSet Ref.
I am trying to do the same through the Java REST toolkit from Rally. It uses the JSON REST API internally, it seems.
When I try to do this with CreateRequest or UpdateRequest, I get an error from the API "Could not set value for Test Set: null"
Is it not possible to update the TestSet of a TestCaseResult (whether existing or newly created)?
Here's some sample code I am using (showing create testcaseresult from existing by changing testset).
//get testcaseresult object
GetRequest tcrReq = new GetRequest("/testcaseresult/12345.js");
tcrReq.setFetch(new Fetch("FormattedID", "Name"));
GetResponse tcrResponse = restApi.get(tcrReq);
//update testcaseresult object with new testset
JsonObject tsRef = new JsonObject();
tsRef.addProperty("_ref", "https://rally1.rallydev.com/slm/webservice/1.39/testset/1029348.js");
tcrResponse.getObject().add("TestSet",tsRef);
tcrResponse.getObject().remove("_ref");
//Create API for new testcaseresult object
CreateRequest createRequest = new CreateRequest("testcaseresult", tcrResponse.getObject());
CreateResponse createResponse = restApi.create(createRequest);
if(createResponse.wasSuccessful()){
System.out.println(createResponse.getObject());
}else{
String[] ss = createResponse.getErrors();
for(int i=0; i<ss.length; i++){
System.out.println(ss[i]);
}
}
Can you please help to understand whether I am doing something wrong or is this a Rally limitation?
I believe the reason that you are getting the "Could not set value for Test Set: null" error message is that there is an "invisible" constraint on TestCaseResults whereby the TestCase to which they are associated must be scheduled into the TestSet of interest, before the TestCaseResult can be assigned that TestSet as an attribute.
Unfortunately, there's no TestSet attribute on TestCases, so you have to query the TestCases collection off of the TestSet, and then loop through that collection to check and see if the parent TestCase is a member of that collection. Once verified that the TestCase is in that TestSet's collection of TestCases, then you can proceed to successfully update a member TestCaseResult with that TestSet attribute of interest. I tested the below and it works as expected.
Here's a code snippet illustrating how to accomplish this:
// Create and configure a new instance of RallyRestApi
// Connection parameters
String rallyURL = "https://rally1.rallydev.com";
String wsapiVersion = "1.38";
String applicationName = "RestExample_UpdateTestSetOnTestCaseResult";
// Credentials
String userName = "user#company.com";
String userPassword = "password";
RallyRestApi restApi = new RallyRestApi(
new URI(rallyURL),
userName,
userPassword);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
// Ref to Test Case Result of Interest
String testCaseResultRef = "/testcaseresult/1234567891.js";
GetRequest testCaseResultRequest = new GetRequest(testCaseResultRef);
GetResponse testCaseResultResponse = restApi.get(testCaseResultRequest);
JsonObject testCaseResultObj = testCaseResultResponse.getObject();
// Get the Test Case Result's Parent Test Case
JsonObject testCase = testCaseResultObj.get("TestCase").getAsJsonObject();
String testCaseRef = testCase.get("_ref").getAsString();
GetRequest testCaseRequest = new GetRequest(testCaseRef);
GetResponse testCaseResponse = restApi.get(testCaseRequest);
JsonObject testCaseObj = testCaseResponse.getObject();
System.out.println(testCaseRef);
// Ref to Test Set of Interest
String testSetRef = "/TestSet/12345678910.js";
// Get the Test Set of interest
GetRequest testSetRequest = new GetRequest(testSetRef);
GetResponse testSetResponse = restApi.get(testSetRequest);
JsonObject testSetObject = testSetResponse.getObject();
// Grab the Test Cases in this Test Set
JsonArray testCasesInTestSet = testSetObject.get("TestCases").getAsJsonArray();
// Loop through and see if our Test Case of interest is a member
boolean testCaseIsInSet = false;
for (int i=0; i<testCasesInTestSet.size(); i++) {
JsonObject thisTestCase = testCasesInTestSet.get(i).getAsJsonObject();
String thisTestCaseRef = thisTestCase.get("_ref").getAsString();
if (thisTestCaseRef.equals(testCaseRef)) {
testCaseIsInSet = true;
}
}
if (testCaseIsInSet) {
// Update Test Set on Existing Test Case Result
try {
//Add Test Set
System.out.println("\nUpdating Existing Test Case Result's Test Set attribute...");
testCaseResultObj.addProperty("TestSet", testSetRef);
UpdateRequest updateExistTestCaseResultRequest = new UpdateRequest(testCaseResultRef, testCaseResultObj);
UpdateResponse updateExistTestCaseResultResponse = restApi.update(updateExistTestCaseResultRequest);
if (updateExistTestCaseResultResponse.wasSuccessful()) {
System.out.println("Updated Test Case Result with new Test Set");
String[] updateExistTestCaseResultWarnings;
updateExistTestCaseResultWarnings = updateExistTestCaseResultResponse.getWarnings();
System.out.println("Warning(s) occurred updating Test Case Result: ");
for (int i=0; i<updateExistTestCaseResultWarnings.length;i++) {
System.out.println(updateExistTestCaseResultWarnings[i]);
}
} else {
String[] updateExistTestCaseResultErrors;
updateExistTestCaseResultErrors = updateExistTestCaseResultResponse.getErrors();
System.out.println("Error occurred updating Test Case Result: ");
for (int i=0; i<updateExistTestCaseResultErrors.length;i++) {
System.out.println(updateExistTestCaseResultErrors[i]);
}
}
} catch (Exception e) {
System.out.println("Exception occurred while updating Tags on existing Test Case: ");
e.printStackTrace();
}
finally {
//Release all resources
restApi.close();
}
} else {
System.out.println("Unable to Update Test Case Result with specified Test Set");
System.out.println("Parent Test Case is not a member of this Test Set");
}
}
When updating the TestSet you can just set the value as its ref- you don't need the wrapper object.
tcrResponse.getObject().add("TestSet", "/testset/1029348.js");

EntityReference has an EntityKey property value that does not match?

I am attempting to add some entities that I have created. When I try and add the entity in question to the set (see code below) I get the following error:
"The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object."
I can't tell what entitykey it's referring to though. Here is the code, there is probably a much better way to pull this off as well:
public Internship CreateInternship(Internship internshipToCreate)
{
try
{
Contact contactToCreate = new Contact();
contactToCreate.Fax = internshipToCreate.contacts.Fax;
contactToCreate.Extension = internshipToCreate.contacts.Extension;
contactToCreate.FirstName = internshipToCreate.contacts.FirstName;
contactToCreate.MiddleName = internshipToCreate.contacts.MiddleName;
contactToCreate.LastName = internshipToCreate.contacts.LastName;
contactToCreate.PhoneNumber = internshipToCreate.contacts.PhoneNumber;
contactToCreate.StreetAddress = internshipToCreate.contacts.StreetAddress;
contactToCreate.PostalCode = internshipToCreate.contacts.PostalCode;
contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;
contactToCreate.statesReference.EntityKey =
new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
contactToCreate.countriesReference.EntityKey =
new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);
_internshipEntities.AddToContactSet(contactToCreate);
_internshipEntities.SaveChanges();
try
{
Availability availabilityToCreate = new Availability();
availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;
_internshipEntities.AddToAvailabilitySet(availabilityToCreate);
_internshipEntities.SaveChanges();
try
{
internshipToCreate.contactsReference.EntityKey =
new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);
internshipToCreate.classificationsReference.EntityKey =
new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);
_internshipEntities.AddToInternshipSet(internshipToCreate); //exception here
_internshipEntities.SaveChanges();
return internshipToCreate;
}
catch(Exception e)
{
throw e;
}
}
catch(Exception e)
{
throw e;
}
}
catch(Exception e)
{
throw e;
}
}
There is no other information given besides the error when I trace through so I'm not even sure which Key is the issue.
EDIT: Here is the version that ended up working:
using (TransactionScope scope = new TransactionScope())
{
try
{
Contact contactToCreate = new Contact();
Availability availabilityToCreate = new Availability();
Internship i = new Internship();
// Set the contact entity values;
contactToCreate.Fax = internshipToCreate.contacts.Fax;
//...
//ommited for brevity
//...
contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;
// Set the contact entity references to existing tables
contactToCreate.statesReference.EntityKey =
new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
contactToCreate.countriesReference.EntityKey =
new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);
// Add contact
_internshipEntities.AddToContactSet(contactToCreate);
// Set the availability entity values;
availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;
// Add availability
_internshipEntities.AddToAvailabilitySet(availabilityToCreate);
//Add contact and availability entities to new internship entity
i.contacts = contactToCreate;
i.availability = availabilityToCreate;
// Set internship entity values;
i.UserID = internshipToCreate.UserID;
//...
//ommited for brevity
//...
i.Created = DateTime.Now;
// Set the internship entity references to existing tables
i.classificationsReference.EntityKey =
new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
i.educationReference.EntityKey =
new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);
// Add internship and save
_internshipEntities.AddToInternshipSet(i);
_internshipEntities.SaveChanges();
//commit transaction
scope.Complete();
return internshipToCreate;
}
catch (Exception e)
{
throw e;
}
}
Hallo,
although I'm not sure what the problem is I have a suggestion. The Internship object that you are passing into method (internshipToCreate) is used to transfer values to other entities (Contact, Availability) that you instantiated inside of the method, and their creation works just fine.
Maybe you should try to do the same with Internship? Create new Internship object and set all values you have by taking them from internshipToCreate object, and than that newly created object pass to the _internshipEntities.AddToInternshipSet method.
It is possible that you've set some values on internshipToCreate object that you needed for other purposes, and that some of those is actually causing the exception.
And, I don't know what you business logic is, but it would be better if you put all under one transaction, because like this it may happen that first two entities are created, and third one not.
This code isn't making a lot of sense to me. In two cases, you're going through an EntityKey when you could just assign an object reference. I.e, change this:
internshipToCreate.contactsReference.EntityKey =
new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);
...to:
internshipToCreate.contacts = contactToCreate;
internshipToCreate.availability = availabilityToCreate;
In the other two cases you seem to be attempting to assign the ID of the object which is already there. These two lines, even if successful, it seems to me, would do nothing:
internshipToCreate.classificationsReference.EntityKey =
new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);
So you can just get rid of them.
What happens when you make these two changes?