Skip to main content

Posts

Showing posts with the label Entity Framework

LINQ: Overview of LINQ and it's advantages and disadvantages

Overview of LINQ: LINQ stands for Language Integrated Query, which is descriptive for where it's used and what it does. LINQ is used for querying data. Here I used the generic term "data" and didn't specified type of data. That's because LINQ can be used to query many different types of data, including SQL, XML, and even objects. It is a Microsoft programming model and methodology that gives formal query capabilities into Microsoft .NET-based programming languages (mainly in C# and VB.Net). LINQ Syntax: LINQ queries can be written through standard query expression or through Lambda expressions. Query expression syntax: var items = from item in Items where item.Price > 10 select item; Lambda expression syntax: var items = Items.Where(c => c.Price > 10).Select(c => c); Type of LINQ: Various type of LINQ available is: ·          LINQ to SQL ·      ...

LINQ: Grouping Data in LINQ using Group By

In this post I’ll explain how one can group data using LINQ GroupBy. GroupBy can be applied on different data types like Generic List, XML, DataTable etc. Syntax of Group By: var result= from c in <collection>     group c by c.<property> into g     select new     {         Key=g.Key,         Value=g     }; LINQ Grouping return partitioned sets of data from a collection based on a given key value, i.e. group employees by designation as: var query = from c in employees         group c by c.Designation; //or same statement can be written in Lambda expression as: var query = employees.GroupBy(c => c.Designation); GroupBy Result: Let’s understand result returned by LINQ grouping operation. The return collection fro...

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

While querying LINQ to Entities with check of Date, one may face the error like 'Date' is not supported in LINQ to Entities…… For example consider following LINQ statement, in which we are trying to get all employees created before today’s date: context.Employees.Where(c => c.CreatedOn >= DateTime .Now.Date ); If you compile this statement, it compiles successfully, however you'll get the error (mentioned below) when this query get executed. “The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported" Reason: Entity Framework doesn't allow the use of the Date member of DateTime inside an LINQ query. And because of the use of .Date member it throws the exception as when query get executed DateTime.Date cannot be converted to SQL statement. Solution: As I mentioned above LINQ to Entities queries are translated to SQ...

Difference between Entity Framework 3.5 and Entity Framework 4.0 (What's New in Entity Framework 4.0?)

The first release of Entity Framework shipped in .NET Framework 3.5 SP1 and Visual Studio 2008. Entity Framework 4 has been released as part of the release of Visual Studio 2010 and .NET Framework 4.0. Entity Framework 4.0 builds on Entity Framework 3.5 in the following areas: Multi-Targeting Support: Entity Data Model Designer and Entity Framework fully support Multi-Targeting capabilities of VS 2010. You can use the designer to continue building your EF 3.5 applications or move forward to EF4. We take care of upgrading / downgrading you to the right EDMX versions and such so that you can use a single IDE to build apps using EF, regardless of the framework version you are targeting. Model-First Development: In addition to the Database-First approach you had in EF 3.5, Entity Data Model designer in Visual Studio 2010 includes Model-First capability to allow you to start from a model, and generate database based on that model. Code-Only Development: Write classes an...

Entity Framework Versus LINQ To SQL (comparison summary)

LINQ to SQL and ADO.NET Entity Framework are extensions of ADO.NET and are introduced to avoid difficulties involved in writing programs using object oriented programming languages to access data residing in RDBMS. LINQ To SQL Entity Framework Complexity LINQ To SQL is easier to use. Entity Framework is more complex compared to LINQ To SQL. DB Server Support LINQ To SQL supports only Microsoft SQL Server. Entity Framework is built on top of ADO.NET data provider model and thus supports all existing ADO.NET data providers  i.e. IBM DB2, Sybase, Oracle, SQL Azure etc. File Type It uses Database Markup Language (DBML) file that contains XML mappings of entities to tables. Entity Framework uses four files EDMX, CSDL, SSDL and MSL. The later three are generated at runtime. Purpose Used for rapid application development. ...

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

While using EF 4.1 code-first to insert data into a SQL Server database. If there is a validation error, EF throws a   DbEntityValidationException whose   EntityValidationsErrors   contains the details of the issue. For example you have set one field as required while creating model, but while saving data in that entity user have supplied Null value to that required field, EF throws a   DbEntityValidationException. There can be many other validation errors as well, and to see what exactly error EF is throwing, you can write you SaveChanges method in try..catch block as: try {     dbContext.SaveChanges(); } catch ( DbEntityValidationException e) {     foreach ( var error in e.EntityValidationErrors)     {          Console .WriteLine( "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:" ,      ...

Walk through to Entity Framework and Edmx file with working sample

One of my friends wanted to learn entity framework and requested me for a running sample of entity framework file. Here I am writing an entity framework sample application to demonstrate the whole idea of entity and we can check how powerful it is while we walk through the sample. Before starting with application part, I have already one DB “ Test_DB ” which I will use in this sample application. You can check I have three tables in the DB to check the relationship between tables let’s see the DB diagram for this DB. Here you can check “ tb_Section ” and “ tb_Class ” are referenced in table “ tb_Student ”. ( One thing you should make sure that table should be properly referenced if you wanted to make full and powerful use of Entity Framework ). So our DB is ready now let’s starting with our application. Open Visual Studio and add Blank Solution and name it as “ TestApplication ”. After that add new ASP.Net Application project to this solution and nam...