CRM Early Bound Entity Classes in Code Vs. .NET Traditional LINQ (Entity Framework)
Hello Folks,
I have been always writing some basic sort of examples on CRM scripting / SDK. This time I have brought down this to .NET core feature called “Entity Framework” versus “CRM SDK”.
This article is useful is people who are quite familiar with LINQ but are new to CRM entity classes.
Let’s take a look at the code comparisons below:
.NET Entity Framework Example:
SQL Table: Student
id | int |
name | varchar(300) |
marks | int |
Now when we generate the .NET Entity Model using .edmx file. We will see the classes have been regenerated with relevant properties as below
public int id { get; set ;}
public string name { get; set ;}
public int marks { get; set ;}
When we start to use this in LINQ implementation, we would generally use Students object and receive the data.
var context = new EntityFrameWorkEntities();
var query = (from o in context.Students select o );
This will result into complete rows of Student Table. Now let’s have a look at the way we would write CRM classes using LINQ. First and foremost we would need to generate classes using CrmSvcUtil.exe.
Below is the screen shot of CrmSvcUtil Screen shot:
Consider a sample entity called “new_Student” with similar fields like we took in above example.
Here’s what it would look like:
CRM SQL table: new_student
new_id | int |
new_name | nvarchar(300) |
new_marks | int |
Here’s the LINQ implementation:
IOrganizationService service = ReturnCrmService();
using (var context = new CrmContext(service))
{
var query = (from o in context.AccountSet select new{ o.Name }.Name);
foreach (string name in query)
{
Console.WriteLine(string.Format("Account Name: {0}", name));
}
Console.ReadKey();
}
By default all CRM entities are post pended with the word “Set”. This is how you can identify the entity names. I have attached the working CRM Project for your quick reference.
Hope this information was helpful.
Cheers,
Apurv