简体   繁体   中英

Dynamics CRM 2011 Custom Entities: creating model that matches my SQL Server foreign and primary key relationships

I have a nice SQL Server DB setup with 2 tables with one to many relationship. Something like this:

TYPE:

ID   Type
===========
1    mammal    
2    fish    
3    insect

PETS:

ID    Name   Foreign Key
=======================
1    Paris   1 (mammal)    
2    Michael 2 (fish)    
3    Mardy   1 (mammal)

How on earth do i recreate this in CRM? I know CRM likes to create its own primary key GUIDs and relalationship fields. I want to keep the integrity of my tabular relationships particularly due to the fact that i will be importing large amounts of data (1million + records) via a script on a regular basis rather than creating the records and relationships via the crm gui. Thanks.

In same situation, I created two entities with additional fields - integer ID. Relationship is CRM default one. If you want to 'keep' ID of 'Type' entity on 'Pets' form, you can add additional hidden field on Pets form (integer - Type_ID). And update this on Type change.

I think that this hidden Type_ID is not necessary if you will use these entities only from scripts. Maybe query performance will not be ideal, but this works :)

Edit: One more thing, if you need to generate IDs in CRM, you have to create plugin for generating unique ID, and which will be executed on entity create.

Hope it helps

Since CRM stores its PKs as GUIDs you can only define FKs only via those GUIDs.

As lazarus stated, create both Pets and PetTypes entities included an ExternalID int field.

Your import script needs to look up both GUIDs for every record to be imported:

read record
lookup PetTypes Guid for PetTypes.ExternalID = [pettype.id from record]
create if not exists
lookup Pets Guid for Pets.ExternalID = [id from record]
create if not exists
set Pets.PetTypes = new EntityReference() with PetTypes.ID (=guid)
save crm record

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM