Friday, April 1, 2016


CRUD Operations using Code First Approach in Dot Net


There are many posts for this topic and samples available which describes how we can perform CRUD operation in .Net using Code First Approach.

So I will, not start in detail how to create database, or perform migrations in code First approach. But would mention following commands which can come handy in migrations:

Pre-Requisite for Code First Approach is EntityFramework and EntityFramework.SqlServer references needs to be added to perform below commands in Package Manager Console.

1. "enable-migrations" to enable migration on the project. 
2. add-migration InitialCreate to create a migration script(*.cs file) that will be executing on database with the new changes or the updated changes.
3. update-database  This command helps us when within a team anyone has already created a migration script from above "add-migration" command in his/her environment then we only need to execute "update-database" or "update-database filename"

Also, don't forget to add the connection string in config file.


Now, lets continue with our main topic of CRUD operations which would focus on many to many relationship table:

To start with this lets take following Entities:


public class ProductType
    {
        public int Id { get; set; }
        public string Title{ get; set; }
    }

public class Product
    {
        public int Id { get; set; }
        public string Name{ get; set; }
        public ProductType ProductType { get; set; }
        public virtual ICollection<Vendor> Vendors
       
    }

public class Vendor
    {
        public int Id { get; set; }
        public string Name{ get; set; }
        public virtual ICollection<Product> Products
       
    }

Now, considering following snipped is added to ProjectContext.cs(Context) file:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
                .HasMany(d => d.Vendors)
                .WithMany(dt => dt.Products)
                .Map(cs =>
                {
                    cs.MapLeftKey("ProductId");
                    cs.MapRightKey("VendorId");
                    cs.ToTable("Products_Vendors");
                });

        }

Now we have our Entity classes ready and migrations(new and udpated changes in entity classes) created and updated to database:

Create Method would be as Follows:

public int Create(Product entity)
        {
            // In case we are updating ID manually based on the tow inputs we are using lock so there is no duplication of values
            lock (_padLock)
            {
                using (db = new ProjectContext(_connectionString))
                {
                 
                    var result = db.Product .Add(entity);

 if (entity.ProductType != null)
                    {
                    db.ObjectStateManager.ChangeObjectState(entity.ProductType, EntityState.Unchanged);
                    }
               
                    if (entity.Vendors != null)
                    {
                        foreach (var item in entity.DeviationTypes)
                        {
                            db.Entry(item).State = EntityState.Unchanged;
                        }
                    }
                    db.SaveChanges();

                    return result.Id;
                }
            }
        }