Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Friday, April 24, 2015

How to solve problem "Unable to load the specified metadata resource." in Entity Framework?


Replace EDMX connection metadata part to
connectionString="metadata=res://*/;

Wednesday, September 24, 2014

How to solve problem while upgrade to EF6: System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

To solve this problem for me is to add EntityFramework.SqlServer.dll into project reference:

System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

Monday, August 25, 2014

How to get sql query from LINQ?

Before getting linq data, using ToString method. Will see the generated SQL.
It is not easy to read and understand.

   var soldOutProducts = 
            from p in products 
            where p.UnitsInStock == 0;

   string sql =  soldOutProducts.ToString();

Wednesday, June 18, 2014

How to solve, Entity Framework return duplicated records from a view if there is no key in the view


1. Make sure the view should be a key there. And marked as Entity key
2. If do not have a key, add one by Row number
ID= ROW_NUMBER() OVER (ORDER BY fieldName DESC)

Do not forget: marked that field as Entity key in Edmx file

Reference:
http://jepsonsblog.blogspot.in/2011/11/enitity-framework-duplicate-rows-in.html?showComment=1348809764880#c1389404724781617559

Monday, June 2, 2014

How to solve problem, Invalid column name 'Discriminator'?

Get this error while upgrade Entity Framework from 5.0 to 6.x.
Install Entity Framework by Nuget. The version got is 6.1

Then get this weird error: Invalid column name 'Discriminator'
All of fields for that table are in the database table, so [NotMapped] is not applicable in here

The solution to me is to revert to 6.02, problem solved.

How to solve problem, "No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'."

Entity framework is using older version 5.0 and on .Net 4.0, need to install a new one.

Go to Package Manager Console:
Run
Install-Package EntityFramework

Wednesday, January 8, 2014

How to solve "Collection was modified; enumeration operation may not execute."?


For IEnumerable collection, need to use ToList first, so you can get same list during the iteration.
                foreach (var qs in one.QuestionSets.ToList())
                {
                    dbc.Questions.DeleteObject(qs.Question);
                }

Tuesday, December 10, 2013

How to solve Entity Framework “Invalid Column Name” problem?

Most of cases, because the schema of current database is different with original one.

Compare two database schema to find out the difference.

How to solve "LINQ to Entities does not recognize the method" problem

Linq will convert syntax into a SQL expression. If any method is not available in SQL query, will get this error.
Such as:
- NQ to Entities does not recognize the method System.Guid Parse(System.String) method, and this method cannot be translated into a store expression.
- NQ to Entities does not recognize the method 'System.String ToString()' method


Solution:
Prepare value before run LINQ query..


Thursday, August 8, 2013

How to solve Entity framework problem: System.InvalidOperationException: Mapping and metadata information could not be found for EntityType entity framework Unable to load the specified metadata resource?


Once you use self-tracking entities, if want to add new one. Please keep using same type.

In one assembly, entity framework only allow one kind mapping. If more than one mapping type, will get above weird error message.

Thursday, May 30, 2013

How to solve "null reference exception" problem when turn off ProxyCreationEnabled in Code first Entity framework?

If turn on lazy loading in entity framework by:
Configuration.ProxyCreationEnabled = false;
Will get null reference exception problem when try to get navigation properties.

Need to do eagerly loading for navigation property by include clause
using (var context = new BloggingContext())
{
    // Load all blogs and related posts
    var blogs1 = context.Blogs
                          .Include(b => b.Posts)
                          .ToList();

}

Inheritance in Entity Framework Code First

public abstract class Person
{
     public int PersonID { get; set; }
     public string Name { get; set; }
}
 
public class StaffMember : Person
{
     public string BadgeNumber { get; set; }
     [ForeignKey("StaffMemberID")]
     public virtual ICollection Appointments { get; set; }
}
 
public class Client : Person
{
     [ForeignKey("ClientID")]
     public virtual ICollection Appointments { get; set; }
}
 

public class MyContext : DbContext
{
     public DbSet People { get; set; }
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
          base.OnModelCreating(modelBuilder);
     }
 
     public void Seed(MyContext context)
     {
          Client c1 = new Client { Name = "Steve Client" };
          StaffMember sm1 = new StaffMember { Name = "Staff 1", BadgeNumber = "1234" };
          StaffMember sm2 = new StaffMember { Name = "Staff 2", BadgeNumber = "5678" };
 
          context.People.Add(c1);
          context.People.Add(sm1);

          context.SaveChanges();
      }
 
      public MyContext()
      {
           Database.SetInitializer(new DropCreateDatabaseAlways());
           this.Seed(this);
      }
}


http://sgordondeveloper.blogspot.ca/2013/02/model-inheritance-with-relationship-in.html

Friday, May 3, 2013

How to change EDMX connection string at runtime in Entity Framework?

Call constructor with connection string parameter like:
var db = new dbContext(connectionstring);

Wednesday, March 27, 2013

How to deal with error "System.Data.MetadataException: Unable to load the specified metadata resource" when move edmx entity file to another project?

Cause: the res:// path changed. Need to be modified

Change connection for edmx file from
metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
To
metadata=res://projectname/foldername.csdl|res://*/Model1.ssdl|res://*/Model1.msl;

Wednesday, January 2, 2013

How to map part of table (only for specific columns) in Entity Framework?


No, you can’t.
The workaround is to create store procedure or view, the map from there

Thursday, December 20, 2012

How to change entity framework connection string dynamically

Use constructor with connection string
http://msdn.microsoft.com/en-us/library/bb739017.aspx

var db = new dbContext(connectionString);

Friday, July 20, 2012

Entity Framework and Open Source

http://weblogs.asp.net/scottgu/archive/2012/07/19/entity-framework-and-open-source.aspx

he Entity Framework source code is today being released under an open source license (Apache 2.0), and the code repository is now hosted on CodePlex (using Git) to further increase development transparency.