none
Identity Return from savechanges but from object type. RRS feed

  • Question

  • Hi, 

    I have written my code as follows

    public int createPerson(PERSON ObjPersonProfile)
            {
                int i = 0;
                if (ObjPersonProfile != null)
                {
                    i = dataAdaptor.save(ObjPersonProfile);
                }

                return i;
            }


     public int save(object oObject)
            {
                try
                {
                    SQLAdaptor ida = new SQLAdaptor();
                    EntityConnectionStringBuilder ecb = ida.efConnstr();
                    ef6Entities pef6Entities = new ef6Entities(ecb.ConnectionString);
                     pef6Entities.AddObject(oObject.GetType().Name + "s", oObject);
                     pef6Entities.SaveChanges();
                }
                catch (Exception ex)
                {
                }
                return 1;
            }

    This is a common save method (http://geekswithblogs.net/danemorgridge/archive/2009/12/02/entity-framework-tip-building-a-reusable-save-method.aspx) which can be used for all entities. But now i need to return the latest id. This can be achieved in normal scenario by <entity>.id. But i am not able to do so beacuse of the object type. Can some one tell me how i can return the new created value?

    Thanks

    Prachin Soparkar


    Thursday, April 16, 2015 10:21 AM

Answers

  • Hello Prachin Soparkar,

    >>But i am not able to do so beacuse of the object type. Can some one tell me how i can return the new created value?

    You could try to use refection since the object type would be obtained at run time, and from the edmx medadata workspace, to obtain these primary key properties, and compare the object type property to check if current property is a PK, if so, set it value and return it, here I made a demo, you could have a try:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Order order = new Order() { OrderID = 3, OrderName = "3" };
    
                var lastID = Save(order);
    
            }
    
    
            private static int Save(object order)
    
            {
    
                var lastID = 0;
    
                using (DFDBEntities db = new DFDBEntities())
    
                {
    
                    ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
    
                    context.AddObject(order.GetType().Name, order);
    
                    context.SaveChanges();
    
                    ReadOnlyMetadataCollection<EdmMember> PKs = (ReadOnlyMetadataCollection<EdmMember>)context.MetadataWorkspace.GetItems(DataSpace.CSpace).FirstOrDefault(i => i.BuiltInTypeKind.ToString() == "EntityType" && i.MetadataProperties["Name"].Value.ToString() == order.GetType().Name).MetadataProperties["KeyMembers"].Value;
    
                    var properties = order.GetType().GetProperties();
    
                    foreach (var property in properties)
    
                    {
    
                        if (PKs.Select(s => s.Name).Contains(property.Name))
    
                        {
    
                            lastID = (int)property.GetValue(order);
    
                        }
    
                    }
    
                }
    
    
    
                return lastID;
    
            }
    
        }

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, April 17, 2015 3:41 AM
    Moderator

All replies

  • Hello Prachin Soparkar,

    >>But i am not able to do so beacuse of the object type. Can some one tell me how i can return the new created value?

    You could try to use refection since the object type would be obtained at run time, and from the edmx medadata workspace, to obtain these primary key properties, and compare the object type property to check if current property is a PK, if so, set it value and return it, here I made a demo, you could have a try:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Order order = new Order() { OrderID = 3, OrderName = "3" };
    
                var lastID = Save(order);
    
            }
    
    
            private static int Save(object order)
    
            {
    
                var lastID = 0;
    
                using (DFDBEntities db = new DFDBEntities())
    
                {
    
                    ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
    
                    context.AddObject(order.GetType().Name, order);
    
                    context.SaveChanges();
    
                    ReadOnlyMetadataCollection<EdmMember> PKs = (ReadOnlyMetadataCollection<EdmMember>)context.MetadataWorkspace.GetItems(DataSpace.CSpace).FirstOrDefault(i => i.BuiltInTypeKind.ToString() == "EntityType" && i.MetadataProperties["Name"].Value.ToString() == order.GetType().Name).MetadataProperties["KeyMembers"].Value;
    
                    var properties = order.GetType().GetProperties();
    
                    foreach (var property in properties)
    
                    {
    
                        if (PKs.Select(s => s.Name).Contains(property.Name))
    
                        {
    
                            lastID = (int)property.GetValue(order);
    
                        }
    
                    }
    
                }
    
    
    
                return lastID;
    
            }
    
        }

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, April 17, 2015 3:41 AM
    Moderator
  • Thanks Fred Bao. It did solve the issue for me. 
    Friday, April 17, 2015 5:35 AM