Showing posts with label Entity Framwork 5. Show all posts
Showing posts with label Entity Framwork 5. Show all posts

Wednesday, October 30, 2013

Unable to update the EntitySet because it has a DefiningQuery and no element exists in the element to support the current operation.

 

Recently I got this error when I try to update table using Entity framework 5.

Cause :Reason for that is the target table does not have the primary key.So need to add the primary key to the target table.

Happy coding Smile

Thursday, October 24, 2013

LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method

 

recently I have used below query to get some filterded details from my table.

dbContext.ttable.Where(r => string.IsNullOrWhiteSpace(r.field))






but when I try to run above code snipped I got


“LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression."”


after few minutes searching found the reason for that.


My query return type is IQueryable<ttable>.So this type is optimized to the deliver an optimized query.In this context all C# methods are not supported.There for I have to change the query like this and then it works Smile


dbContext.table.where.(!(b.field == null || r. b.field.Trim() == ""))

A processor named 'T4VSHost' could not be found for the directive named 'CleanupBehavior'. error VS2012 and EF 5

I install entity framework 5 to my 2012 project and create the EDMX using the already created Database schema.but project build was failed mentioning above error.so I search and found below solution.

Step 1: Go to below LOcation

s1

Step 2 :

Open EF.Utility.CS.ttinclude file and remove code part from it and save .

“<#@ CleanupBehavior Processor="T4VSHost" CleanupAfterProcessingTemplate="true" #>”

2

How Add a new attribute to an Entity Framework 5 model class(Data Base First)

I have entity class which was generated from entity framework 5 db first approach.So I needed to add another property to this class because I need to do few track the instance state for some reason.

There are few ways to this in Entity framework.

Approach 1 :Because entity class is partial can created another class same, same name space and add the new  property

Approach 2 : Add new property to directly to the entity and marked its as not mapped entity using NotMapped attribute.

In my scenario I used approach  2.

 

        public string Warehouse { get; set; }
public string ProductGroup { get; set; }
public string RolPackageNo { get; set; }

public virtual ICollection<Tab_AODLines> Tab_AODLines { get; set; }
public virtual ICollection<Tab_PickListLines> Tab_PickListLines { get; set; }
public virtual ICollection<Tab_SplitLines> Tab_SplitLines { get; set; }

//new property
[NotMapped]
public bool HasChanged { get; set; }



Of course you'll need to have these two using statements at the top your Entity Framework class to get things to compile.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

 

Bingoooooo Smile