- In this walkthrough we will learn How to fix the Entity Framework error "does not have a primary key defined and no valid primary key could be inferred".

- In the case you design an Entity Type in the Entity Framework's conceptual entity model, based not on a sql table, but on a sql view, or in the case your table does not have a primary key defined, the entity framework will try to infer which column can be the unique KEY of the entity.
You cannot state a primary key for an SQL SERVER view, so if you create an Entity Framework entity based on a view, you may have an issue here.
Usually the Entity Framework will succeed inferring the primary key: if the SQL view is the result of a JOIN and you bring some primary key between the selected columns of the view, it will take that field as the unique KEY of the entity.
The error "does not have a primary key defined and no valid primary key could be inferred" tells us that the Entity Framework couldn't infer the primary key.
- In our example, we see that the entity CRM_USER_ACTIVITIES, based on the view of the same name, does not have a primary key:

- We received the error message because there is not field at the view that the Entity Framework can recognize as an original Primary Key of some table.In our example, NEW_IDENTIFIER is not a key, and ACTIVITYID, although a primary key of the table LETTERBASE, is camouflaged here inside the "GUID" column:

- To solve the problem, let's wrap the entire select inside another select which will load the Primary Key of the LETTERBASE table we spoke of before, ACTIVITYID, using an INNER JOIN, so that the Entity Framework will recognize the key: therefore state BEFORE the query the code

- And at the end of the query , let's close the JOIN defining the ON clause using the primary key:

- Now save the view using ALTER and rerun the Entity Framework rebuilding the Visual Studio project:

- Just press at Finish after selecting the CRM_USER_ACTIVITIES from the "Views" tree item:

- Another approach to solve the problem, is to add a new column to the SQL SERVER view, called ID. This column will be unique, because we'll insert in it a DENSE_RANK over the field that we know is unique:

That's all...Enjoy Dynamics CRMכתב: כרמל שוורצמן
A Step-By-Step Walk-Through CRM 2011 designed for Begginer and Professional CRM Developers
Microsoft Dynamics CRM 2011
Thursday, October 3, 2013
Step-By-Step How to fix the Entity Framework error "no valid primary key could be inferred"
by Carmel Schvartzman
Wednesday, September 18, 2013
How to create an On Demand Workflow in Dynamics CRM
by Carmel Schvartzman
- In this walkthrough we will learn Step-By-Step how to create an On Demand Workflow in Dynamics CRM 2011. A Workflow is a custom process which runs in the application background and whose aim is to automatize bussiness and processes tasks , and is built on successive steps. After its creation, a Workflow can be triggered by entities creation, assignament or deletion, by changes on the entity, and even can be called on demand. For Example, we can use a Workflow to make a Letter each time the user wants to issue one. This is the created Letter, built by the OnDemand Workflow, and appended to an Account:

- To create the new Workflow, go to Customize the System, and then to Processes:

- Click on the New button:

- Next, type a relevant Process Name and select the Entity you want the Workflow apply to:

In our case, the Entity is Account. - Next, select the Workflow category:

- Next, because we want that the workflow will be run when it is demanded to run, check the "On Demand" field:

Notice that you can also choose to run the Workflow when the record is deleted, or reassigned, when its status changes , or when some field is updated. - Now we will add steps to the workflow. To create a Letter on demand, let's select "Create Record":

- Type a description for the Step, and go to "Set Properties" in order to design the Letter:

- Now you can design what you want the Letter to be, for example, let's type a Subject and also select the Sender of the Letter. Next, add some Description and the Letter's Date:

- When you are done, Save and Close the dialog:

- Don't forget to activate the Workflow:

- Now let's create a new Account and see how it works:

- Now go to the Accounts list, and search for the Account we just created, to see the Letter attached to it. Go to the Advanced Search, and set the following search: "ACCOUNT NAME - CONTAINS - SEBASTIAN": (finally click on Results to see the required record)

- We have found the new Account we just created:

- On the Account, press "Run Workflow": that means it will run On Demand:

- On the Workflows dialog, select the Workflow we just designed:

- Now that we started the Wokflow, we want to check if it has finished:

- Lets check whether the workflow has finished its work:

The status of the workflow is "Succeded": that means its done. - Now lets see the Letter created. So open the Account and then go to the Activities:

- There we will see the Letter that the Workflow created automatically On Demand:
- And Welcome to the Letter issued On Demand:

That's all...Enjoy Dynamics CRMכתב: כרמל שוורצמן CRM
Subscribe to:
Posts (Atom)
