EntityFramework and Migration with database




 <connectionStrings>

    <add name="MainModel" connectionString="server=Localhost;database=dbMVCProject;Integrated Security = true;" providerName="System.Data.SqlClient"/>

</connectionStrings>

after adding your connection string in web.config Open Package Manager Console

write :

Install-Package EntityFramework

After installation... Create the Class of collection of database tables In Class "MainModel".


MainModel.cs

------ start -----------

using System.Data.Entity; // This for DbContext
using System.ComponentModel.DataAnnotations; // this for DataAnnotations


namespace MVCProject.Models
{
    public class MainModel : DbContext
    {
        public DbSet<tbl_product> tbl_product { get; set; }
        public DbSet<tbl_SliderMaster> tbl_SliderMaster { get; set; }
    }
    public class tbl_product
    {
        [Key]
        public int ProductId { get; set; }

        public string ProductCode { get; set; }

        public string ProductName { get; set; }

        public string Duration { get; set; }

        public string Cost { get; set; }

        public string Description { get; set; }

        public DateTime? CreateDate { get; set; }

        public DateTime? LastUpdate { get; set; }

        public bool IsActive { get; set; }

        public bool IsDelete { get; set; }

        public string Producturl { get; set; }

        public string UnitPrice { get; set; }

        public int? Brandid { get; set; }

        public int? subproductId { get; set; }

    }

    public class tbl_SliderMaster
    {
        [Key]
        public int Id { get; set; }

        public string Image { get; set; }

        public string ImageName { get; set; }

        public string Heading { get; set; }

        public string Description { get; set; }

        public bool IsActive { get; set; }

        public bool IsDelete { get; set; }
    }
}

------ end -----------

Then again Open Package Manager Console

enable-migrations

add-migration Initialcreate

update-database

Now your Database Automatically Created in your Available DBMS


_________________________________________________

 If you facing any problem Then you can send me message or mail on
 my mail ID : sumitshekhawat92@gmail.com 
_________________________________________________

 ****Thanks For Reading ****

Comments

Popular posts from this blog

Tree view in winforms using c#

how to Replace null value with 0 Using C#

how to fetch all HTML Table Records and modify all records accordingly using jquery and Javascript