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;
                }
            }
        }








Friday, June 14, 2013

SharePoint 2010 Setup unable to proceed due to a pending system restart

Hi friends,

This is the article which i found on Donal Conlon blog, which helped me to resolve the issue of setup failure due to pending system restart which got resolved by renaming only single system registry key as explained in Donal's article link is given below:


The registry key which needs to be renamed is “PendingFileRenameOperations”  to “PendingFileRenameOperations1” which we can find in following location "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager".

Then just proceed the Sharepoint 2010 installation without restarting the system and it works!!!!


Thursday, June 6, 2013

Convert Object to XML in C#


Add following name space:

using System.Xml; using System.Xml.Serialization;


Then add below code in class file

/// <summary>
/// Serializes a class to xml text
/// </summary>
/// <param name="item">Class to be converted to XML</param>
/// <returns>XML string representing class</returns>
public static string SerializeObjectToXML(object item)
{
    try
    {
        string xmlText;
        //Get the type of the object
        Type objectType = item.GetType();
        //create serializer object based on the object type
        XmlSerializer xmlSerializer = new XmlSerializer(objectType);
        //Create a memory stream handle the data
        MemoryStream memoryStream = new MemoryStream();
        //Create an XML Text writer to serialize data to
        using (XmlTextWriter xmlTextWriter =
            new XmlTextWriter(memoryStream, Encoding.UTF8)
                { Formatting = Formatting.Indented })
        {
            //convert the object to xml data
            xmlSerializer.Serialize(xmlTextWriter, item);
            //Get reference to memory stream
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            //Convert memory byte array into xml text
            xmlText = new UTF8Encoding().GetString(memoryStream.ToArray());
            //clean up memory stream
            memoryStream.Dispose();
            return xmlText;
        }
    }
    catch (Exception e)
    {
        //There are a number of reasons why this function may fail
        //usually because some of the data on the class cannot
        //be serialized.
        System.Diagnostics.Debug.Write(e.ToString());
        return string.empty;
    }
}