Blog

My blog serves as a way for me to post solutions to problems I solve at work or during the long hours of freelance work.

Creating a Website in MVC 4

 

I'm creating a new website in MVC 4. This page will serve as a way to keep track of problems and solutions.

Need to make sure when first publishing to the web server to create the application in IIS. Otherwise, the routing doesn't work correctly.

 

Entity Framework Code First -

http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx

 I was trying to get the Code First to work with MVC 4, but I wasn't able to get it to work correctly.

Error -

"The entity type EdmMetadata is not part of the model for the current context"

 

MVC 3 way of doing code first doesn't work in MVC 4

 

 

I was working on saving updates to the database and ran into this error -

 Error -

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

 

Solution -

Remove [Bind(Exclude = "Id")] from your model class.

 

Currently trying to figure out how to use file upload in MVC. 

 

Here is an example of how to setup using Code First in MVC 4

 

Context Class -

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

namespace MvcApplication2.Models
{
    public class ProductContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Supplier> Suppliers { get; set; }
       
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Supplier>()
                .Property(s => s.Name)
                .IsRequired();
        }
    }
}

 

Initializer Class -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;

namespace MvcApplication2.Models
{
    public class ProductInitializer : DropCreateDatabaseAlways<ProductContext>
    {
        protected override void Seed(ProductContext context)
        {
            var products = new List<Product>
            {
                new Product
                {
                    Name = "test",
                    Price = "100.00",
                    Description = "test product",
                    Keywords = "test, test"                   
                },

                new Product
                {
                    Name = "test2",
                    Price = "1.00",
                    Description = "test product2",
                    Keywords = "test2, test2"                   
                }

            };

            products.ForEach(d => context.Products.Add(d));
        }
    }
}

 

Global asax -

using System.Data.Entity;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using MvcApplication2.Models;

namespace MvcApplication2
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            Database.SetInitializer<ProductContext>(new ProductInitializer());


            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            BundleTable.Bundles.RegisterTemplateBundles();
        }
    }
}

 

 

MvcApplication2.zip (14.03 mb)


RecentPosts