Google Chrome Problem Fix Tip

Navigate to chrome://settings/ -->>-- Click on "Show advanced settings"  

-->>-- Scroll to bottom of settings page to "System" section. Uncheck "Use hardware acceleration when available".

Entity Framework Code First Step By Step

Step 1 : Download Entity Framework Library;

Tools -> NuGet Package Manager -> Manage NuGet Packages Manager For Soluiton.

Select Online on left side and seacrh entity and download.


Step 2: Create your classes; (That step not have any relation with your DB logic)

public class Product
{
     public int ProductId { get; set;}
     [Required,StringLength(100)]
     public string ProductName { get; set;}
     public int CategoryId { get; set;} 
     public virtual Category Category { get;  set; }

     // That will not shown on your DB useful for password again on member form
     [NotMapped]
     public int DontMapTest { get; set; } 
}

public class Category
{
     public int CategoryId { get; set;} 
     public string CategoryName { get; set;}
     public virtual List<Product> Product { get; set; }
}


Step 3 : Create your Context class : (That step for DB relation and must take DBContext inheritance)

public class YourContextClassName : DbContext
{
    public override void OnModelCreating(DbModelBuilder modelBuilder)
    {
          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }    
    public YourContextClassName()
    {
          Database.Connection.ConnectionString = "server=yourservername;database=yourdatabasename;user=yourusername;password=yourpassword";
    }
    public DbSet<Product> Products { get; set;}
    public DbSet<Category> Categories { get; set;}
}


Step 4 : Enable Migration

Tools -> NuGet Package Manager -> Package Manager Console

PM> enable-migrations (Press Enter) That will create migrations folder on your project

Change AutomaticMigrationsEnabled = false to true in your Configuration file.


Step 5 : 

Tools -> NuGet Package Manager -> Package Manager Console

PM> update-database (Press Enter) You will see on your DB Manager your database created as you wish.

Sometimes you will need to use update-database -force



Entity Framework 6 Working with Transactions

using (var context = new BloggingContext())
{
    using (var dbContextTransaction = context.Database.BeginTransaction())
    {
        try
        {
            context.Database.ExecuteSqlCommand(
                @"UPDATE Blogs SET Rating = 5" +
                    " WHERE Name LIKE '%Entity Framework%'"
                );

            var query = context.Posts.Where(p => p.Blog.Rating >= 5);
            foreach (var post in query)
            {
                post.Title += "[Cool Blog]";
            }

            context.SaveChanges();

            dbContextTransaction.Commit();
        }
        catch (Exception)
        {
            dbContextTransaction.Rollback();
        }
    }
}