mscrm import update columnmapping reference attribute - import

I work on Dynamics 365 and I need to develop a dynamics update of records using the Import Entity.
I manage to make a dynamics import to create record into Dynamics 365 but for update I have that message :
"This attribute is not mapped as a reference attribute. However, you have included a ReferenceMap for it. Fix this inconsistency, and then import this data map again."
Well, I don't find any documentation even in SDK even on web even on Microsoft website. I hope to find help from you guys ;)
See my code about columnmapping of my reference ID mapping :
Entity colMapping = new Entity(CrmColumnmapping.EntityLogicalName);
colMapping.Attributes.Add(CrmColumnmapping.Fields.SourceAttributeName, dc.Caption);
colMapping.Attributes.Add(CrmColumnmapping.Fields.SourceEntityName, "MySource");
colMapping.Attributes.Add(CrmColumnmapping.Fields.TargetAttributeName, dc.ColumnName);
colMapping.Attributes.Add(CrmColumnmapping.Fields.TargetEntityName, dt.TableName);
colMapping.Attributes.Add(CrmColumnmapping.Lookups.ImportMapId, new EntityReference(CrmImportmap.EntityLogicalName, importMap.Id));
colMapping.Attributes.Add(CrmColumnmapping.OptionSets.ProcessCode, new OptionSetValue(CrmColumnmapping.OptionSets.ProcessCodeValues.Option1_Process));
colMapping.Id = service.Create(colMapping);
Entity parentLookupMapping = new Entity(CrmLookupmapping.EntityLogicalName);
parentLookupMapping.Attributes.Add(CrmLookupmapping.Lookups.ColumnMappingId, colMapping.ToEntityReference());
parentLookupMapping.Attributes.Add(CrmLookupmapping.OptionSets.ProcessCode, new OptionSetValue(CrmLookupmapping.OptionSets.ProcessCodeValues.Option1_Process));
parentLookupMapping.Attributes.Add(CrmLookupmapping.Fields.LookUpAttributeName, dc.Namespace + "id");
parentLookupMapping.Attributes.Add(CrmLookupmapping.Fields.LookUpEntityName, dc.Namespace);
parentLookupMapping.Attributes.Add(CrmLookupmapping.OptionSets.LookUpSourceCode, new OptionSetValue(CrmLookupmapping.OptionSets.LookUpSourceCodeValues.Option2_System));
parentLookupMapping.Id = service.Create(parentLookupMapping);
Entity currentLookUpMapping = new Entity(CrmLookupmapping.EntityLogicalName);
currentLookUpMapping.Attributes.Add(CrmLookupmapping.Lookups.ColumnMappingId, colMapping.ToEntityReference());
currentLookUpMapping.Attributes.Add(CrmLookupmapping.OptionSets.ProcessCode, new OptionSetValue(CrmLookupmapping.OptionSets.ProcessCodeValues.Option1_Process));
currentLookUpMapping.Attributes.Add(CrmLookupmapping.Fields.LookUpAttributeName, dc.ColumnName);
currentLookUpMapping.Attributes.Add(CrmLookupmapping.Fields.LookUpEntityName, dt.TableName);
currentLookUpMapping.Attributes.Add(CrmLookupmapping.OptionSets.LookUpSourceCode, new OptionSetValue(CrmLookupmapping.OptionSets.LookUpSourceCodeValues.Option1_Source));
currentLookUpMapping.Id = service.Create(currentLookUpMapping);
See my code about import :
Entity import = new Entity(CrmImport.EntityLogicalName);
import.Attributes.Add(CrmImport.OptionSets.ModeCode, new OptionSetValue(CrmImport.OptionSets.ModeCodeValues.Option2_Update));
import.Attributes.Add(CrmImport.Fields.Name, "UpdateImport");
import.Id = service.Create(import);
If someone know how to map the reference attribute (ex : systemuserid) to manage to make a dynamics update import, it will be appreciated.
Thanks a lot

Related

DropDownChoice gets duplicate choices

i have an issue with a DropDownChoice (the DDC) component. The situation is as following: I want to create a simple registry page for a contest. So i need a team with participants. I have created a form (which is a composition of different panels/forms) on which you can enter the name, age and 'position in the game'. Then you press the 'add participant' button and the participant should appear in the DropDownChoice.
I am new to Apache Wicket and actually i am glad i get the form to show on the screen and to see that the participants are actually added to the DDC. But here comes the issue: All of the participants in the DDC are 'turned into' the last one added. In other words: suppose i create the participant Jeff. Jeff gets added to the DDC, no problem. Then i create Mike. When i add Mike to the DDC and look at the available participants, Jeff seems to have been turned into Mike. So at this point, i do have 2 participants in my team, but the first one,Jeff, i suddenly Mike as well. And it is not only the property which is displayed that has changed. It is the complete content of the Participant object that turns into Mike.
Now if i would like to add Janine, Jeff and Mike would both turn into Janine and i would have 3 'Janine participants' in my DDC. I ll add the code of the 'TeamForm', which i believe is the most relevant. If needed, i can put more code on.
package com.tvh.tournamentregistry.form;
import com.tvh.tournamentregistry.model.Participant;
import com.tvh.tournamentregistry.model.Team;
import com.tvh.tournamentregistry.panel.ParticipantPanel;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
public class TeamForm extends Form {
public TeamForm(String id){
super(id);
final Team team = new Team();
CompoundPropertyModel<Team> teamModel = new CompoundPropertyModel<Team>(team);
setModel(teamModel);
add(new TextField("name"));
Model<Participant> participantModel = new Model<Participant>();
ChoiceRenderer<Participant> teamViewRenderer = new ChoiceRenderer<Participant>("firstname");
final DropDownChoice<Participant> teamView = new DropDownChoice<Participant>("players",participantModel, team.getPlayers(), teamViewRenderer){
#Override
protected boolean wantOnSelectionChangedNotifications() {
return true;
}
};
add(teamView);
final ParticipantPanel participantPanel = new ParticipantPanel("participantpanel");
add(participantPanel);
Button addParticipant = new Button("addparticipant"){
#Override
public void onSubmit() {
Participant participant = (Participant) participantPanel.getModel().getObject();
team.getPlayers().add(participant);
teamView.setChoices(team.getPlayers());
teamView.render();
participantPanel.clear();
}
};
addParticipant.setDefaultFormProcessing(false);
add(addParticipant);
}
#Override
protected void onSubmit() {
super.onSubmit(); //To change body of generated methods, choose Tools | Templates.
}
}
I have debugged the little application and what i saw was quite disturbing. i put a breakpoint on
Participant participant = (Participant) participantPanel.getModel().getObject();
after adding 2 participants, so i could have a look at the
team.getPlayers()
method which returns a list of participants. The model that gets returned by the paricipantspanel (which is a custom method, passing the model from the form in that panel) is correct. It returns the participant that i have entered in my form. But when i look in the team list, even before my debugger get to that line, i can see that all the other participants have 'changed' already. And i am not touching the list, only adding new participants.
Any thoughs anybody? Thanks! If this was absolutely not clear, please ask!
Each time you add a new participant, the ParticipantPanel must have its model "re-initialized" otherwise its model object references the same object all the time.
In detail:
Participant a.
On first render, your panel uses this participant so on add, it adds it to you list.
After that, while re-rendering, the model object of the panel is still point to participant a. So changes affect the old object. That's why your dropdown has your single participant repeated.
Try the following:
Instead of using getter, use PropertyModel
new DropDownChoice<Participant>("players",participantModel, team.getPlayers(), teamViewRenderer)
change to
new DropDownChoice<Participant>("players",participantModel, new PropertyModel(team, "players"), teamViewRenderer)
Specify idExpression in ChoiceRenderer
new ChoiceRenderer<Participant>("firstname");
change to
new ChoiceRenderer<Participant>("firstname", "id");

Entity Framework - the edm type is mapped more than one

I have a simple Entity data model (using VS2010) that I reverse engineered from a simple SQL Server database that contains three unrelated tables.
I try to save data to the database using the following code:
var dbOptions = new ARC_WHENTECHModel.TEMP_LANDED_WHENTECH_OPTION_POSITION();
//first map fields required for standard di processing
dbOptions.OPTION_POSITION_SOURCE_ID = webSvcOption.TDR + "_" + webSvcOption.CNTR + "_" + webSvcOption.CRV;
dbOptions.INSERTED_DT = DateTime.Now;
dbOptions.CURRENT_IND = "Y";
//now map the data we've pulled from the web service call
dbOptions.CA = webSvcOption.CA;
dbOptions.CDTP = webSvcOption.CDTP;
dbOptions.CMD = webSvcOption.CMD;
dbOptions.CNTR = webSvcOption.CNTR;
dbOptions.CPP = webSvcOption.CPP;
dbOptions.PDTP = webSvcOption.PDTP;
dbOptions.SPR = webSvcOption.SPR;
dbOptions.TDR = webSvcOption.TDR;
context.AddToTEMP_LANDED_WHENTECH_OPTION_POSITION(dbOptions);
context.SaveChanges();
but I get the following exception:
An EdmType cannot be mapped to CLR classes multiple times. The EdmType 'RDLMServiceTier.TEMP_LANDED_WHENTECH_FUTURES_POSITION' is mapped more than once.
at the following line (base.AddObject) within the designer.cs code:
[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
public void AddToTEMP_LANDED_WHENTECH_OPTION_POSITION(TEMP_LANDED_WHENTECH_OPTION_POSITION tEMP_LANDED_WHENTECH_OPTION_POSITION)
{
base.AddObject("TEMP_LANDED_WHENTECH_OPTION_POSITION", tEMP_LANDED_WHENTECH_OPTION_POSITION);
}
Could anyone please advise what I may be doing wrong? I don't have much experience with Entity Framework (it's always "just worked" in the past). I guess the next step may be to reflect the EF code but that's going to be quite tricky because I'm debugging on a remote machine that I don't have great scope for installing extra software.
Thanks
Rob.
Gert answered this correctly - there was another copy of the model file present.
I just had a similar problem, and the specific line that caused it was:
[EdmComplexTypeAttribute(NamespaceName = "MyModel", Name = "MyDuplicatedClassName")]
Once the duplicated name was fixed everything worked fine.

How to retrieve dynamic content from database?

I'm evaluating java-based cms and selecting one as our cms ,now I'm learning dotcms , I need to know how to retrieve content from db like traditional jsp/bo does,I'm new to dotcms, the official documents only tell how to add static content but dynamic content , say running a sql and getting the wanted data ,then putting them into pages. We are doing an internal website where employees can browse news, events, colleagues information etc which managed through a cms, the information is definitely dynamic and updated regularly. We plan to use spring mvc on the project. Any ideas on the question?
thank you.
To get this to work you need to do a few things:
If you want to use a different database, then you can add a new resource to the conf/Catalina/localhost/ROOT.xml file. If you want to use the dotCMS database to host the additional tables then you can skip this step.
From within your java code you can get a database connection using the DbConnectionFactory class. Now you can read the data from the database. Here's an example:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Connection conn = DbConnectionFactory.getConnection();
Statement selectStatement;
try {
selectStatement = conn.createStatement();
try {
selectStatement.execute("SELECT * FROM your_table WHERE your_where_clause etc...");
ResultSet result = selectStatement.getResultSet();
if (result.next()) {
.. do your stuff here...
// for example:
// Long dbId = result.getLong("Id");
// String stringField = result.getString("stringFieldName");
// int intField = result.getInt("intFieldName");
} finally {
selectStatement.close();
}
} catch (SQLException e1) {
// Log the error here
}
}
If you want to use this data in velocity you'll need to create a viewtool. Read more about that here: http://dotcms.com/docs/latest/DynamicPluginsViewtool

Loading particular peoperties for entity and assigning it as reference to another entity

Example:
I am entering a new invoice. For this invoice I need to enter a customer. Lets assume that we retrieved a list of customers:
var list = Context.Set<Customer>().ToList();
Here I see two issues:
1) I do not need to bring all information for customer, I only need Id, Code and Name
2) Customer in current DbContext is read-only, so it would be nice if it is possible to tell DbContext not to monitor their states, to improve performance.
Questions:
1) Can we load only partial data for customer, but still be able to assign it to Invoice (see code bellow)?
2) Can we tell DbContext not to monitor Customers for changes, and still be able to do this:
Invoice.Customer = CustomerList[10];
There's not a direct way to do exactly what you want, but you might be able to achieve your goals with some compromise.
I do not need to bring all information for customer, I only need Id,
Code and Name
There isn't a way for EF to create a partially loaded entity, but you could create an anonymous type:
Context.Customers.Select(c => new {Id = c.CustomerId, Code = c.Code, Name = c.Name}).Tolist()
If you could live with the new anonymous type then use that, or you could then iterate through that list, creating actual customer objects.
Customer in current DbContext is read-only, so it would be nice if it
is possible to tell DbContext not to monitor their states, to improve
performance.
EF provides an Extension of AsNoTracking() which will do exactly what you're looking for:
var list = Context.Set<Customer>().AsNoTracking().ToList();
Depending on what you choose from above, the following code may change, but this code does achieve what you're looking for. Partially loads the customer, but still allows you to attach the customer to the invoice.
Note: You'll need to attach the customer to your context before you can use it, and then setting it to a state of Unchanged will prevent it from overwriting exiting data.
m = new Model();
var list = m.Customers.Select(c => new {Id = c.CustomerId, Code = c.Code, Name = c.Name});
List<Customer> customerList = new List<Customer>();
foreach (var item in list)
{
customerList.Add(new Customer()
{
CustomerId = item.Id,
Code = item.Code,
Name = item.Name
});
}
Invoice i = new Invoice();
var customer = customerList.First();
m.Customers.Attach(customer);
m.Entry(customer).State = EntityState.Unchanged;
i.Customer = customer;
m.Invoices.Add(i);
m.SaveChanges();

How to save child relationship entities in the Entity Framework

I have an Entity Framework v1 project. I have two entities (Roles and Permissions), which have a many-to-many relationship with each other. I pass in a object to be saved (through a WCF call, I do not create it from a context myself), which has new entries in the many-to-many relationship.
I use "context.ApplyPropertyChanges" to update the record with the new properties. I know that this does not update relationships though. I attempt to either do a ChildCollection.Add(relatedObject); or ChildCollection.Attach(relatedObject).
When I use the "Add" method, I get the error that: The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key.
When I use the "Attach" method, I get the error that: The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key.
I am getting quite frustrated, and I think I can hear the Entity Framework laughing at me.
Does anyone know how I can resolve this?
MyRole x = context.Roles.FirstOrDefault(a => a.RoleId == this.RoleId);
context.ApplyPropertyChanges("Roles", this);
foreach (MyPermission p in this.Permissions)
{
x.Permissions.Add(p);
// ^ or v
x.Permissions.Attach(p);
}
context.SaveChanges();
Thanks.
Wow. After 20 or so straight hours on this problem, I'm starting to hate the Entity Framework. Here is the code that appears to be working currently. I would appreciate any advice on how to make this more streamlined.
I did rework the WCF service so that there is only the one data context. Thanks Craig.
Then I had to change the code to the following:
MyRole x = context.Roles.FirstOrDefault(a => a.RoleId == this.RoleId);
if (x == null) // inserting
{
MyApplication t = this.Application;
this.Application = null;
context.Attach(t);
this.Application = t;
}
else // updating
{
context.ApplyPropertyChanges("Roles", this);
x.Permissions.Load();
IEnumerable<Guid> oldPerms = x.Permissions.Select(y => y.PermissionId);
List<MyPermission> newPerms = this.Permissions.Where(y => !oldPerms.Contains(y.PermissionId)).ToList();
IEnumerable<Guid> curPerms = this.Permissions.Select(y => y.PermissionId);
List<MyPermission> deletedPerms = x.Permissions.Where(y => !curPerms.Contains(y.PermissionId)).ToList();
// new
foreach (MyPermission p in newPerms)
{
x.Permissions.Add(context.Permissions.First(z => z.PermissionId == p.PermissionId));
}
// deleted
foreach (MyPermission p in deletedPerms)
{
x.Permissions.Remove(context.Permissions.First(z => z.PermissionId == p.PermissionId));
}
}
You are using multiple ObjectContexts concurrently (the variable context and whereever this came from). Don't do that. It will only make things very difficult for you. Use one ObjectContext at a time.
I can give more specific advice if you show more code.
I suspect you are getting the errors because the ObjectContext thinks you are trying to add a new entity but finds it already has a EntityKey. I use the AttachTo method of the ObjectContext to attach my already existing entities to their EntitySet. I have had results generating my entities from stubs or hitting the database. This way when you add the entity to the navigation property on your entity, the ObjectContext finds the entity in it's EntitySet and knows it is an existing entity and not a new one. I don't know if this is clear. I could post some code if it would help. As Mr Stuntz said in his answer, posting more of your code would help.