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

Thursday, 27 November 2014

Entity Framework 5 Code First - Customizing Migrations

You want to make changes to your Models and Database, you have auto migration enabled and you run:

update-database -force

you get this error:

'FK_dbo.{TABLENAME}_dbo.{OTHER_TABLENAME}_otherID' is not a constraint.
Could not drop constraint. See previous errors.

You sh*t a brick.

Solution:

Run this:

Add-Migration Update{You Model Name}Class

This creates a file in Migrations containing a Partial Class outlining what changes the Framework is attempting as a "best guess"

Remove any unwanted changes in this file.

E.g DropForeignKey("dbo.{TABLENAME}", "otherID", "dbo.{OTHER TABLE}");

Now run:

Update-Database –TargetMigration:Update{You Model Name}Class

NOW READ THIS, TWICE!

http://msdn.microsoft.com/en-gb/data/jj591621.aspx#customizing

And if none of that works

If you're still getting errors, and trust me, it happens. Then I recommend backing everything up and starting with a clean slate.

I followed the instructions here http://stackoverflow.com/questions/11679385/reset-entity-framework-migrations from Todd. Thank's Todd :)

  1. Delete the migrations folder in your project
  2. Delete the __MigrationHistory table in your database (may be under system tables)
  3. In PM Console run: Enable-Migrations -EnableAutomaticMigrations -Force
  4. In PM Console run: Add-Migration Initial
  5. In PM Console run: update-databse -TargetMigration:Initial

Wednesday, 4 June 2014

MVC4 Entity Framework and Decimal Precision

This just caught me out today so hopefully it will help someone else.

Stack Overflow answer here http://stackoverflow.com/questions/3504660/decimal-precision-and-scale-in-ef-code-first

If you have a database field with the spec decimal(18,5) the Entity Framework will still round the number to 2 decimal places by default, so if you enter a number e.g. 15.42700 it will be saved to your database as 15.42000, not very helpful eh. Anyway to fix it you need to override OnModelCreating in your DBContext Class

public class EFDbContext : DbContext { protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { modelBuilder.Entity().Property(object => object.property).HasPrecision(12, 10); base.OnModelCreating(modelBuilder); } }

Friday, 21 February 2014

Searching Model Data based on string and integer field types

Scenario

You need to add a search function based on model data based on searching both string and int datatypes. If you try the obvious method in your LINQ query as just converting your int field ToString you end up with an error similar to this:

ToString is not supported in linq to entities

Or even this bad boy:

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

After trawling around the forums for a good hour or so I found the following solution. Hope it helps someone else too.

 public ActionResult Search(string search = null)  
     {  
       int intSearch;  
       if (!Int32.TryParse(search, out intSearch))  
       {  
       }  
       IEnumerable<Car> cars = context.Cars.Where(  
            c => search == null  
              || c.IntegerField == intSearch  
              || c.Manufacturer.Contains(search)  
              || c.Model.Contains(search)             
             );  
       cars = cars.OrderBy(c => c.Id).ToList();  
       ViewBag.Search = search;  
       return View(cars);  
     }  

Wednesday, 18 December 2013

ASP.NET MVC 4 Entity Framework (EF) Code First

I'm finding this a very fast and efficient way of creating .NET MVC Web Applications but still find I have to keep looking up the initial stages of setting up Entity Framework Migrations. With that in mind I'll be updating this post periodically with summary notes on what's involved.

The best article I've found on this subject so far is on dominikgorecki.com entitled "Code First Entity Framework with MVC4 and Visual Studio 2012".