• Entity Framework provides Code-First approach from Entity Framework 4.0. Code-First is mainly useful for First design model for business domain requirement instead of database Design. In the Code-First approach, you can focus on the business domain design and start creating classes as per your business domain requirement rather than design your database first and then create the classes which match your database design. Entity framework code first automatically create database when run your application.
      • Code First Example:
      • Instead of designing database tables first, let's start creating classes for our blogger domain, as and when needed. First, we will create two simple Articlepost and Category classes as shown below.

       

      public class ArticlePost

          {

              public Guid Id { get; set; }

              public string Title { get; set; }

              public string Content { get; set; }

              public DateTime CreatedDate { get; set; }

              public virtual Category Category { get; set; }

          }

          public class Category

          {

              public Guid Id { get; set; }

              public string Name { get; set; }

              public virtual ICollection<ArticlePost> ArticlePosts { get; set; }

          }

       

      Now, we are done with the initial business domain requirement classes for our blogger application. Code-First approach also requires context class which should be derived from DbContext.

      Following Create a context class which derive from DBContext class and exposes DbSet properties for the types that you want to be part of the model, e.g. ArticlePost and Category class, in this case. DbSet is a collection of entity classes so we have given property name as plural of entity name like ArticlePosts and Categories.

      public partial class BlogContext : DbContext

          {

              public BlogContext()

                  : base("DefaultConnection")

              {

              }

       

              public DbSet<ArticlePost> ArticlePosts { get; set; }

              public DbSet<Category> Categories { get; set; }

       

          }

      In the above example, you allowed EF to create a database for you. In many cases you may want to give database name and connection string where you want to create database  than specifying a database connection string in the application configuration file (or web.config file) as shown below:

      <connectionStrings>

          <add name="DefaultConnection"

               providerName="System.Data.SqlClient"

          connectionString="data source=(local);initial catalog=ArticleDb;persist security info=True;user id=sa;password=sa@123;multipleactiveresultsets=True;App=EntityFramework"/>   

        </connectionStrings>

      Now, we are done with the required classes and database connection string  for the code-first approach. We will now add a category using context class as shown below.

      public ActionResult Index()

              {

                  BlogContext db = new BlogContext();

                  Category Category = new Models.Category();

                  Category.Id = new Guid();

                  Category.Name = "SQL Server";

                  db.Categories.Add(Category);

                  db.SaveChanges();

                return  View();

                 

              }

      Now if you run the application, you will be surprised to see that the application runs successfully .let’s See below automatically database created and one Category is successfully inserted into the database.



      Thanks.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.