Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Entity Framework is not re-creating back my tables after namespace name changed

$
0
0

Hi, I have changed all my namespace using few ways including "FIND and REPLACE" all.

But after I did that, I realize that my code is not re-creating back all the tables initialized by my models class. This is the error:

Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.

in my global.asax, I suspect this line is not running

Database.SetInitializer(new ProductDatabaseInitializer());

at

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Initialize the product database.
            Database.SetInitializer(new ProductDatabaseInitializer());


            // Add Routes.
            RegisterCustomRoutes(RouteTable.Routes);
        }

In my ProductDatabaseInitializer.cs , my code is below:

using System.Collections.Generic;
using System.Data.Entity;

namespace myProject.Models
{
  public class ProductDatabaseInitializer : DropCreateDatabaseIfModelChanges<ProductContext>
  {
    protected override void Seed(ProductContext context)
    {
      GetProducts().ForEach(p => context.Products.Add(p));
    }

    private static List<Product> GetProducts()
    {
      var products = new List<Product> {
                new Product
                {
                    ProductID = 1,
                    UnitPrice = 22.50,
               },
                new Product
                {
                    ProductID = 2,
                    ImagePath="carearly.png",
                    UnitPrice = 115.95,
               }
            };

      return products;
    }
  }
}


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>