Entity Framework Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/category/technology/n-tier-apps/entity-framework/ Production Grade Technical Solutions | Data Encryption and Public Cloud Expert Tue, 26 May 2015 18:55:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://www.anujvarma.com/wp-content/uploads/anujtech.png Entity Framework Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/category/technology/n-tier-apps/entity-framework/ 32 32 Multiple FROM statements in a LINQ expression https://www.anujvarma.com/multiple-from-statements-in-a-linq-expression/ https://www.anujvarma.com/multiple-from-statements-in-a-linq-expression/#respond Tue, 26 May 2015 18:55:53 +0000 http://www.anujvarma.com/?p=3130 Multiple “from” statements  are like nested foreach statements. MSDN example:     var scoreQuery = from student in students                         from score in student.Scores                            where score > 90                            select new { […]

The post Multiple FROM statements in a LINQ expression appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
Multiple “from” statements  are like nested foreach statements. MSDN example:

 

  var scoreQuery = from student in students
                         from score in student.Scores
                            where score > 90
                            select new { Last = student.LastName, score };

 

 

This is the equivalent of:

SomeDupCollection<string, decimal> nameScore = new SomeDupCollection<string, float>();
foreach(Student curStudent in students)
{
   foreach(Score curScore in curStudent.scores)
   {
      if (curScore > 90)
      {
         nameScore.Add(curStudent.LastName, curScore);
      }
   }
}

The post Multiple FROM statements in a LINQ expression appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/multiple-from-statements-in-a-linq-expression/feed/ 0
FindAsync with non-primary key value https://www.anujvarma.com/findasync-with-non-primary-key-value/ https://www.anujvarma.com/findasync-with-non-primary-key-value/#respond Wed, 20 May 2015 16:10:06 +0000 http://www.anujvarma.com/?p=3123 var foos = await db.Foos.Where(x => x.UserId == userId).ToListAsync();

The post FindAsync with non-primary key value appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
var foos = await db.Foos.Where(x => x.UserId == userId).ToListAsync();

The post FindAsync with non-primary key value appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/findasync-with-non-primary-key-value/feed/ 0
Quick way to print out generated sql https://www.anujvarma.com/quick-way-to-print-out-generated-sql/ https://www.anujvarma.com/quick-way-to-print-out-generated-sql/#respond Mon, 11 May 2015 15:35:32 +0000 http://www.anujvarma.com/?p=3107 If you are working from an EF context, the context has its own LOG. This can be written out to your development console (see example below). context.Log = Console.Out; DataClassesDataContext […]

The post Quick way to print out generated sql appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
If you are working from an EF context, the context has its own LOG. This can be written out to your development console (see example below).

context.Log = Console.Out;

DataClassesDataContext context = new DataClassesDataContext();
IEnumerable<Order> result = context.Orders;
var product = result.Where(x => x.OrderID == 12);
context.Log = Console.Out;

The post Quick way to print out generated sql appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/quick-way-to-print-out-generated-sql/feed/ 0
Visualizing EDMX files (entity framework data model) https://www.anujvarma.com/visualizing-edmx-files-entity-framework-data-model/ https://www.anujvarma.com/visualizing-edmx-files-entity-framework-data-model/#respond Wed, 15 Apr 2015 15:43:09 +0000 http://www.anujvarma.com/?p=3007 Ever wanted to reverse engineer a code-first Entity framework model ? Or simply generate a diagram from an existing model?  Try the Entity Framework Power Tools.   Power Tools

The post Visualizing EDMX files (entity framework data model) appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
Ever wanted to reverse engineer a code-first Entity framework model ? Or simply generate a diagram from an existing model?  Try the Entity Framework Power Tools.

 

Power Tools

The post Visualizing EDMX files (entity framework data model) appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/visualizing-edmx-files-entity-framework-data-model/feed/ 0
Converting a normal collection to IEnumerable https://www.anujvarma.com/converting-a-normal-collection-to-ienumerable/ https://www.anujvarma.com/converting-a-normal-collection-to-ienumerable/#respond Fri, 25 Jul 2014 22:08:17 +0000 http://www.anujvarma.com/?p=2646 Suppose you are stuck with a namevaluecollection – it is not of type IQueryable or IEnumerable. So – none of your LINQ queries will work on it. Convert it to […]

The post Converting a normal collection to IEnumerable appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
Suppose you are stuck with a namevaluecollection – it is not of type IQueryable or IEnumerable. So – none of your LINQ queries will work on it.

Convert it to IEnumerable<T>

IEnumerable<string> canBeQueried = nvc.OfType<string>();
// To convert it to a string array  
string[] myArr = canBeQueried.Cast<String>().ToArray();

Convert it to IQueryable<T>

IQueryable queryable = nvc.AsQueryable();
 
 

Summary – Once you have these converted to either an IQueryable or an IEnumerable, you can execute your normal LINQ queries against them.

The post Converting a normal collection to IEnumerable appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/converting-a-normal-collection-to-ienumerable/feed/ 0
Update EDMX model with database changes https://www.anujvarma.com/update-edmx-model-with-database-changes/ https://www.anujvarma.com/update-edmx-model-with-database-changes/#respond Fri, 11 Jul 2014 23:39:58 +0000 http://www.anujvarma.com/?p=2625 Open the EDMX file within Visual Studio – use an Open With command from the right click menu Open With -> ADO.NET Entity Data Model Designer Right click on the […]

The post Update EDMX model with database changes appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
  • Open the EDMX file within Visual Studio – use an Open With command from the right click menu
  • Open With -> ADO.NET Entity Data Model Designer
  • Right click on the designer surface of the EDMX designer and click Update Model From Database..
  •   update_model_from_db

    The post Update EDMX model with database changes appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    https://www.anujvarma.com/update-edmx-model-with-database-changes/feed/ 0
    Linqer–Convert SQL statements to LINQ https://www.anujvarma.com/linqerconvert-sql-statements-to-linq/ https://www.anujvarma.com/linqerconvert-sql-statements-to-linq/#respond Sat, 14 Jun 2014 00:25:54 +0000 http://www.anujvarma.com/?p=2591 LINQPad, the most popular LINQ tool out there , does not convert SQL statements into their equivalent LINQ syntax. However, a free tool called Linqer does just that. Linqer takes […]

    The post Linqer–Convert SQL statements to LINQ appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    LINQPad, the most popular LINQ tool out there , does not convert SQL statements into their equivalent LINQ syntax. However, a free tool called Linqer does just that. Linqer takes an SQL expression and turns it into a LINQ query.

    There were a few confusing steps in setting up the tool.

    1. Basically, it needs to have a datasource set up. It needs  this in the form of an ado.net connection string (the same string you would use in an app.config or a web.config file).  Setting up the datasource was a bit tricky – since it insisted on generating DBML files before it would accept the ado.net datasource. Once you provide it with a path to place the dbml file – it happily configures the datasource for use.
    2. Linqer will generate the DBML (LinqtoSQL mapping files) for you.
    3. Now, you are ready to INPUT your sql statements – and see their equivalent LINQ statements as the output (as shown below).

     

    linqer2

    The post Linqer–Convert SQL statements to LINQ appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    https://www.anujvarma.com/linqerconvert-sql-statements-to-linq/feed/ 0
    Inheriting an existing Entity Framework Implementation https://www.anujvarma.com/inheriting-an-existing-entity-framework-implementation/ https://www.anujvarma.com/inheriting-an-existing-entity-framework-implementation/#respond Sat, 31 May 2014 17:43:06 +0000 http://www.anujvarma.com/?p=2562 Several quirks may pop up – the project may not work seamlessly in your local environment. Entity Framework generates a slew of files – most notably its mapping file (with […]

    The post Inheriting an existing Entity Framework Implementation appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    Several quirks may pop up – the project may not work seamlessly in your local environment. Entity Framework generates a slew of files – most notably its mapping file (with an edmx extension) – which captures a snapshot of the underlying database.

    First,  I got the commonly encountered multiple web.configs error (Error 3)

    Error 3 It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.
    Workaround – For some reason, web.config gets copied into subfolders – Find and delete all subfolders web.configs.

    Next, I encountered  EF mapping errors – Mapping of CLR type to EDM type is ambiguous

    Workaround – Make sure you update nuget packages (get EF framework) and regenerate the EDMX  .

    On compilation, I encountered : Type or namespace objects could not be found

    Workaround – Make sure you add a reference to System.data.entity.

    Summary

    Am sure I am missing a list of other common errors encountered in starting out with an existing EF codebase, but these are the ones I ran into.

    The post Inheriting an existing Entity Framework Implementation appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    https://www.anujvarma.com/inheriting-an-existing-entity-framework-implementation/feed/ 0
    Entity Framework–where to put the data folder https://www.anujvarma.com/entity-frameworkwhere-to-put-the-data-folder/ https://www.anujvarma.com/entity-frameworkwhere-to-put-the-data-folder/#respond Fri, 30 May 2014 22:40:46 +0000 http://www.anujvarma.com/?p=2559 In your folder structure, where shoul you put the edmx data folder? As all EDMX files are representations (abstractions) of some relational database, they belong in the Models folder. The […]

    The post Entity Framework–where to put the data folder appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    In your folder structure, where shoul you put the edmx data folder?

    1. As all EDMX files are representations (abstractions) of some relational database, they belong in the Models folder.
    2. The one exception would be file based datasources (mda files, excel files, SQL CE mdf files…) – which could be placed in the App_Data folder.

    The post Entity Framework–where to put the data folder appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

    ]]>
    https://www.anujvarma.com/entity-frameworkwhere-to-put-the-data-folder/feed/ 0