All About ASP.NET and ASP.NET Core 2 Hosting BLOG

Tutorial and Articles about ASP.NET and the latest ASP.NET Core

ASP.NET MVC 5 Hosting - ASPHostPortal :: How to Perform Code First Migration in MVC 5

clock July 16, 2014 08:39 by author Jervis

In today post, I will explain Code Migration in MVC application. This migration helps you to update and change the data model that you’ve created for application and you don't need to re-create or drop your database.

 

How to Apply Code First Migration

The data model in the database usually changes after developing the new application. When the data model changes, it becomes out of sync with the database. We've configured our Entity Framework in the app to drop and recreate the data model automatically. When any type of change is done in the entity classes or in the DbContext class, the existing database is deleted and the new database is created that matches the model and sees it with test data.

When the application runs in production, we do not want to lose everything each time we make changes like adding a column. The Code First Migration solved this problem by enabling the code first to update the database instead of dropping or recreating.

The procedure is following:

1. Disable the Database Initializer in the Web.Config file.

The following is the example:

<!--<contexts>

      <context type="Vag_Infotech.DAL.CollegeDbContext, Vag_Infotech">
        <databaseInitializer type="Vag_Infotech.DAL.CollegeDatabaseInitializer, Vag_Infotech"></databaseInitializer>
      </context>
</contexts>--> 

And also you need to change your Global.asax file:

// Database.SetInitializer(new CollegeDatabaseInitializer()); 

2. Change your database name in your web.config file:

<connectionStrings
  <add name="CollegeDbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=VInfotechNew;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</
connectionStrings> 

3. Open the Package Manager Console and enter this command:

enable-migration

add-migration InitialCreate 

Then, you will see the solution explorer below:

How to Edit Seed Method

Now, we add new code for updating our data model in this method. When we do not use the migration, the data model is dropped or re-created during the execution of the application each time, but now the data is retained after database changes.

Here are the steps to edit seed method:

1. Modify your configuration.cs file with this code:

namespace Vag_Infotech.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Vag_Infotech.Models;
 
    internal sealed class Configuration : DbMigrationsConfiguration<Vag_Infotech.DAL.CollegeDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }
 
        protected override void Seed(Vag_Infotech.DAL.CollegeDbContext context)
        {
            var New_Students = new List<Student>
            {
                new Student{FirstName="Louis",LastName="Fordsen",
                    EnrollmentDate=DateTime.Parse("2009-07-01")},
                new Student{FirstName="David ",LastName="Roger",
                    EnrollmentDate=DateTime.Parse("2009-07-01")},
                new Student{FirstName="Mike",LastName="Upson",
                    EnrollmentDate=DateTime.Parse("2009-07-05")},
            };
            New_Students.ForEach(ns => context.Students.AddOrUpdate(p => p.LastName, ns));
            context.SaveChanges();
 
            var New_Courses = new List<Course>
            {
                new Course{CourseID=201,Name="MCA",Credit=3,},
                new Course{CourseID=202,Name="M.Sc.IT",Credit=2,},
                new Course{CourseID=203,Name="M.Tech.CS",Credit=2,},
                new Course{CourseID=204,Name="M.Tech.IT",Credit=4,}
            };
            New_Courses.ForEach(ns => context.Courses.AddOrUpdate(p => p.Name, ns));
            context.SaveChanges();
 
            var New_Enrollments = new List<Enrollment>
            {
                new Enrollment{
                    StudentID= New_Students.Single(ns=>ns.FirstName=="Louis").ID,
                    CourseID=New_Courses.Single(nc=>nc.Name=="MCA").CourseID,
                    Grade=StudentGrade.A
                },
                new Enrollment{
                    StudentID= New_Students.Single(ns=>ns.FirstName=="David").ID,
                    CourseID=New_Courses.Single(nc=>nc.Name=="M.Tech.IT").CourseID,
                    Grade=StudentGrade.B
                },
                new Enrollment{
                    StudentID= New_Students.Single(ns=>ns.FirstName=="Mike").ID,
                    CourseID=New_Courses.Single(nc=>nc.Name=="M.Tech.CS").CourseID,
                    Grade=StudentGrade.D
                },
            };
 
            foreach (Enrollment et in New_Enrollments)
            {
                var NewEnrollDb = context.Enrollments.Where(
                    ns => ns.Student.ID == et.StudentID &&
                        ns.Course.CourseID == et.CourseID).SingleOrDefault();
                if (NewEnrollDb == null)
                {
                    context.Enrollments.Add(et);
                }
            }
            context.SaveChanges();
        }
    }
}

In the code above the Seed() takes the context object as an input argument and in the code the object is used to add the entities to the database. We can see how the code creates the collection for the database for each entity type and then saves it by SaveChanges() and adds it through the DbSet property. It is not necessary to call the SaveChanges() after each group of entities.

2. Build the solution

How to Execute the Migration

Here are steps:

1. Enter this command on Package Manager Console:

Update-database

2. Run the application and you’ll see the change



ASP.Net Hosting – ASPHostPortal.com :: Adding Email Confirmation to ASP.NET Identity in MVC 5

clock February 19, 2014 09:14 by author Diego

One of the mainline features about ASP.NET Identity is to make it easy to Adding Email Confirmation. This process will send an email to the user with a link they can click on to confirm their registration and log in to the system. Prior to confirmation they will not be able to log in.

 

This post shows how you can adding email confirmation to ASP.NET Identity in MVC 5.

First I started by creating a new MVC 5 application.  You no longer select whether you want an Internet or Intranet application, you just select the MVC template and select the authentication type you want to use. For an Internet type application you select Individual User Accounts.  Once your web application is created open IdentityModels.cs in the Models directory.  You will see a class called ApplicationUser which is analogues to the UserProfile in SimpleMembership.  It is an empty class that inherits from IdentityUser which has these properties.

You must to modify the user information to store a the confirmation token and a flag indicating whether confirmation was completed or not.

Now let’s implement the method in the AccountMembershipService class:

 

Here, we’re setting the confirmationGuid to user. This is the GUID stored in the database that uniquely identifies the user. We then set the verifyUrl to the Verify action on the Account controller – passing the confirmationGuid as the ID parameter. The redirect to the RegisterStepTwo action just displays a view to the user that tells them to look for the email to complete the registration process.

Once the user gets the email they click on the link that will take us back to the controller action RegisterConfirmation. With that configured, when the user clicks the register button, they’ll be sent an email with the confirmation link it, and they’ll be redirected to the confirmation page. If we find a user we set IsConfirmed to true and return true from the method; otherwise we return false. If the user is confirmed they will be able to log in.


That is all there is to setting up email confirmation using ASP.NET Identity in MVC 5.
Thanks.

 



ASPHostPortal.com Proudly Announces ASP.NET MVC 5 Hosting

clock July 12, 2013 08:44 by author Mike

ASPHostPortal.com is a premiere web hosting company that specializes in Windows and ASP.NET-based hosting, proudly announces the new Microsoft product, ASP.NET MVC 5 hosting to all new and existing customers.

ASP.NET MVC 5 is the latest update to Microsoft's popular MVC (Model-View-Controller) technology - an established web application framework. MVC enables developers to build dynamic, data-driven web sites. ASP.NET MVC 5 adds sophisticated features like single page applications, mobile optimization, adaptive rendering, and more. Here are some new features of ASP.NET MVC 5:

- ASP.NET Identity
- Bootstrap in the MVC template
- Authentication Filters
- Filter overrides

“We pride ourselves on offering the most up to date Microsoft services. We're pleased to launch this product today on our hosting environment” said Dean Thomas, Manager at ASPHostPortal.com. “We have always had a great appreciation for the products that Microsoft offers. With the launched of ASP.NET MVC 5 hosting services, we hope that developers and our existing clients can try this new features.”

ASPHostPortal.com is one of the Microsoft recommended hosting partner that provide most stable and reliable web hosting platform. With the new launch of ASP.NET MVC 5 into its feature, it will continue to keep ASPHostPortal as one of the front runners in the web hosting market. For more information about new ASP.NET MVC 5, please visit http://www.asphostportal.com.

About ASPHostPortal.com:
ASPHostPortal.com is a hosting company that best support in Windows and ASP.NET-based hosting. Services include shared hosting, reseller hosting, and sharepoint hosting, with specialty in ASP.NET, SQL Server, and architecting highly scalable solutions. As a leading small to mid-sized business web hosting provider, ASPHostPortal strive to offer the most technologically advanced hosting solutions available to all customers across the world. Security, reliability, and performance are at the core of hosting operations to ensure each site and/or application hosted is highly secured and performs at optimum level.



Cheap ASP.NET 4.5 Hosting

We’re a company that works differently to most. Value is what we output and help our customers achieve, not how much money we put in the bank. It’s not because we are altruistic. It’s based on an even simpler principle. "Do good things, and good things will come to you".

Success for us is something that is continually experienced, not something that is reached. For us it is all about the experience – more than the journey. Life is a continual experience. We see the Internet as being an incredible amplifier to the experience of life for all of us. It can help humanity come together to explode in knowledge exploration and discussion. It is continual enlightenment of new ideas, experiences, and passions


Author Link

 photo ahp banner aspnet-01_zps87l92lcl.png

 

Corporate Address (Location)

ASPHostPortal
170 W 56th Street, Suite 121
New York, NY 10019
United States

Tag cloud

Sign in