I'm trying to create a custom facets to insert data into sitecore9 XDB through Xconnect for this, I have followed the blog and XconnectDoc
But when I'm trying to add data into contact custom facet- I'm getting error as The type of this instance does not correspond to any type in the model as shown in the below snapshot. However, I'm able to add facet data into the XDB for the buildin sitecore facets.
Here is the code Snippet for adding data into custom facet along with build in facet
private static void AddContact()
{
using (var client = GetClient())
{
//Multiple Identifiers can be added as below
var identifiers = new ContactIdentifier[]
{
new ContactIdentifier("user15", "SSkSudheer15", ContactIdentifierType.Known),
new ContactIdentifier("person15", "SSkSuggu15", ContactIdentifierType.Known)
};
Contact contact = new Contact(identifiers);
client.AddContact(contact);
// Optional thing to add based on the requirement
PersonalInformation personalInfoFacet = new PersonalInformation()
{
FirstName = "Sudheer15",
LastName = "Suggu15"
};
FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);
client.SetFacet(reference, personalInfoFacet);
//client.SetFacet<PersonalInformation>(contact, PersonalInformation.DefaultFacetKey, personalInfoFacet);
EmailAddressList emailFacet = new EmailAddressList(new EmailAddress("[email protected]", true), "email");
client.SetFacet(contact, emailFacet);
var carDetails = new CarDetails()
{
CarName = "Benz",
Model = "Mecedes",
year = 2018
};
//FacetReference refernce = new FacetReference(contact, CarDetails.FacetName);
//client.SetFacet(reference, carDetails);
//client.SetFacet<CarDetails>(contact, CarDetails.FacetName, carDetails);
client.SetFacet<CarDetails>(new FacetReference(contact, CarDetails.FacetName), carDetails);
client.Submit();
}
}
And the Model code snippet
I have added facet to a custom model and generated json file and dropped it in a different location in Xconnect root folder.
Custom Model code snippet:
public class XconnectPOCModel
{
public static XdbModel Model { get; } = BuildModel();
private static XdbModel BuildModel()
{
var builder = new XdbModelBuilder("XconnectPOCCarModel", new XdbModelVersion(1, 0));
builder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
builder.DefineFacet<Contact, CarDetails>(CarDetails.FacetName);
return builder.BuildModel();
}
}