Saturday

Exception: System Data Entity Infrastructure DbUpdateException

Introduction

I was working on MVC application using Entity Framework, MVC, Jquery. I hit the Save button to save corresponding values into database, but this time did not go fine as usual, I got the given error An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code.

Error Description

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

Solution

When I started working to resolve issue, debugging deeply and came across of stack trace message then I found that this issue was due to database table identity off.

Table Identity off

I just set table Identity on and again tried to save relevant values into database, this time it worked fine for me.
Table Identity On

Conclusion

Another very good experience of MVC, Entity Framework, Jquery application. Every time, in every new application often comes new issues. From these issues, we learnt a new lesson every time and smoothing way to next time. Above, I have explained in detail how can we resolve issue of An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code. If you still find the same issue, please don't forget to write me here.

Suggested Reading


Sunday

Error CS1963: An expression tree may not contain a dynamic operation

Introduction

I was working on a MVC Application (Razor Engine) using with Entity Framework. I triggered an error not able to populate entities while binding the controls with the database. I have written below detail of error as follows:

Error Description

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1963: An expression tree may not contain a dynamic operation

TempData vs ViewData vs ViewBag

Introduction

Today, I am explaining about TempData vs ViewData vs Viewbag while working in MVC. All three (TempData, ViewData,  ViewBag) terms are used to pass data from controller to View. These all have different  capability to preserve data while passing from Controller to View using MVC application.

ViewBag

  1. ViewBag is used dynamically to pass the data from controller to view.
  2. It is being introduced since C# 4.0 .Net Framework.
  3. ViewData is property of ControllerBase Class in from .Net Framework 4.5 C#.
  4. ViewBag life span is very short. It only lies under current request.
  5. If it redirects then its value becomes null.
  6. It does not required to typecasting for getting data from Controller to View.
  7. ViewBag is slower than ViewData.

Jquery Validation: Email Validation using Jquery

Introduction

This is my second post on Jquery Validation, In my previous post, I explained in detail about Numeric Validation using Jquery, now I am going to explain about Email Validation using Jquery.

Input Box to input Email




Jquery Validation: Numeric Validation using Jquery

Introduction

Today, I am going to explain about numeric validation using Jquery. Jquery plays a great role to validate data at client side without refreshing whole the page. To make your application more user friendly, interactive and soothing then Jquery plays a vital role to fulfill your dreams.
Data Input Text Box

Number :  

Saturday

Entity Framework Examples:Entity Framework Code First Examples

Introduction

Entity Framework is an ORM (Object Relational Mapper) which enables to developer cut off code lines. ADO.Net Entity Framework is now every C#/VB apps necessity to use in MVC, ASP.Net, and Windows applications etc. Here is explained operations with code first approach.
Main Operations (CRUD) in Entity Framework is demonstrated below with code snippet.

Pre-Requisites

If you need these snippet into your machine then require following VS 2012/2013, .Net Framework 4.5 and MS SQL Server.

Installing EF NuGet Package

Right click on Reference folder of your respective project to install Entity Framework NuGet Package and select "Manage NuGet Packages"

Installing through Package Manger Console

Alternatively, you can install Entity Framework  through Package Manger Console.
PM> Install-Package EntityFramework

Download Latest Entity Framework

Here is an official link https://www.nuget.org/packages/EntityFramework/ where you can download latest Entity Framework package.

Entity Framework Version History

Here is the complete story of all Entity Framework Versions. Top most in the list are latest versions.

EntityFramework 7.0.0-beta4
EntityFramework 6.1.3 (this version)
EntityFramework 6.1.3-beta1
EntityFramework 6.1.2
EntityFramework 6.1.2-beta2
EntityFramework 6.1.2-beta1
EntityFramework 6.1.1
EntityFramework 6.1.1
EntityFramework 6.1.0
EntityFramework 6.1.0
EntityFramework 6.1.0
EntityFramework 6.0.2
EntityFramework 6.0.2
EntityFramework 6.0.1
EntityFramework 6.0.0
EntityFramework 6.0.0-rc1
EntityFramework 6.0.0-beta1
EntityFramework 6.0.0-alpha3
EntityFramework 6.0.0-alpha2
EntityFramework 6.0.0-alpha1
EntityFramework 5.0.0
EntityFramework 5.0.0-rc
EntityFramework 5.0.0-beta2
EntityFramework 5.0.0-beta1
EntityFramework 4.3.1
EntityFramework 4.3.0
EntityFramework 4.3.0-beta1
EntityFramework 4.2.0
EntityFramework 4.1.10715
EntityFramework 4.1.10331
EntityFramework 4.1.10311

USE EntityFramework connection in ADO.Net Connection

Use Namespace

using System.Data.EntityClient

Use following code snippet to use Entity Framework connection in ADO.Net Connection

string _entityConnString = ConfigurationManager.ConnectionStrings["db_MyDatabase"].ConnectionString;
string _AdoConnString = new EntityConnectionStringBuilder(_entityConnString).ProviderConnectionString;

How to take Maximum Table ID in Database Table using Entity Framework

_context is object of Entity Framework dbContext

Entity Framework dbContext

Delclaring Object of dbContext

Model1 _context = new Model1();
Int32 _TopicIDFK = _context.myTable.Select(x => x.TableID).Max();

How to Delete Records from Database table using Enity Framework

var _item = (from c in _context.mytable.where(x=> x.id = 10) select c).FirstOrDefault();
_context.tblmyTable.Attach(_item);
_context.tblmyTable.Remove(_item);
_context.SaveChanges();

How to Save Records in Database Table using Entity Framework

var _obj = new tblmyTable();
_obj.TableID = 0;
_obj.Name = txtName.Text.Trim();
_context.tblmyTable.Add(_obj);
_context.Entry(_obj).State = EntityState.Added;
_context.SaveChanges();

How to Update/Modify Records in Database Table using Entity Framework

var _obj = new tblmyTable();
_obj = (from c in _context.tblmyTable.Where(x => x.ID == 10) select c).FirstOrDefault();
obj.ID = 10;
_obj.Name = txtName.Text.Trim();

_context.tblmyTable.Add(_obj);
_context.Entry(_obj).State = EntityState.Modified;
_context.SaveChanges();
How to Check Existing Records from database Table using Entity Framework
bool existsOption = _dtOptions.Select().ToList().Exists(row => row["Options"].ToString() == "technology");

How to run ADO.Net SQL Query using Entity Framework

string _sql = "select * from tblmyTable where ID in (" + _ids + ")";
var _img = _context.Set().SqlQuery(_sql);

How to take First Value from Database Table using Entity Framework

_IdQues = _context.tblmyTable.Select(u => u.QuestionIDPK).First();

How to count number of records using Entity Framework

_countQues = _context.tblmyTable.Where(u => u.ClassIDFK == _class && u.SubjectIDFK == 
_subject && u.ChapterIDFK == _chap && u.QuizIDFK == _quiz && u.isRecStatus == true).Count();

Rename Column Name in Database Table using Entity Framework

var query = (from c in _context.tblmyTable.Where(x => x.ID > 5).OrderBy(x => x.CountryIDPK)
select new {Code = c.CountryIDPK, c.CountryName }).ToList();

How to take Distinct Records from database Table using Entity Framework

var result = EFContext.TestAddresses.Select(m => m.Name).Distinct();

How to Pattern Matching using Entity Framework

var _results = from a in mycontainer.users where a.fullname.Contains(mySearchText)
select a.fullname;

Summary

Entity Framework provides developer to code access capability so eliminate code lines. Now every .net framework (c#, Vb.net) developer is primary requirement to use Entity Framework in your application (MVC, ASP.Net, Windows applications). Major operations (CRUD) , count records, Maximum number in table, Column Rename etc of Entity Framework are explained above with coding snippet in great depth.
Suggested Reading

Friday

Entity Framework Error: Validation failed for one or more entities

Introduction

While working on C# app using Entity Framework, I got the issue Validation failed for one or more entities. Today, I have provided a complete solution to over come issue Validation failed for one or more entities while playing with Entity Framework (MS SQL Server database) using C#, MVC, ASP.Net, VN.net etc.