locked
Get actual entity from ObjectStateEntry RRS feed

  • Question

  • Hi all,

    With the following extension method I should get the actual db name of an entity (still not tested):

            public static string GetTableName<T>(this ObjectContext context) where T : class
            {
                string sql = context.CreateObjectSet<T>().ToTraceString();
                Regex regex = new Regex("FROM (?<table>.*) AS");
                Match match = regex.Match(sql);
    
                string table = match.Groups["table"].Value;
                return table;
            }

    However my problem is that I have objects of type ObjectStateEntry and I need to to somehow cast them into my own entities for the following code to work:

    tableName = this.GetTableName<CustomerType>(),

    Obviously I can't use CustomerType because it all depends on the entity type.

    Is there a way?

    Thanks,

    Ivan

    Thursday, May 31, 2012 3:55 AM

Answers

  • Hi Cryo75,

    I have tried to use reflection to realize it, please refer to the code below, I can get the table name in my test.

    namespace TestConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (NorthwindEntities context = new NorthwindEntities())
                {               
                    Customer cus = context.Customers.First();
    
                    ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(cus);
                    Type t = entry.Entity.GetType();
                    MethodInfo method = typeof(ExtensionMethod).GetMethod("GetTableName");
                    MethodInfo genericMethod = method.MakeGenericMethod(t);
                    string table = genericMethod.Invoke(null, new object[]{context}).ToString();
                    Console.WriteLine(table);
                    Console.Read();
                }
            }
        }
    
        public static class ExtensionMethod
        {
            public static string GetTableName<T>(this ObjectContext context) where T : class
            {
                string sql = context.CreateObjectSet<T>().ToTraceString();
                Regex regex = new Regex("FROM (?<table>.*) AS");
                Match match = regex.Match(sql);
    
                string table = match.Groups["table"].Value;
                return table;
    
            }
        } 
    
    }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cryo75 Wednesday, June 6, 2012 8:05 AM
    Sunday, June 3, 2012 6:11 AM

All replies

  • Hi Cryo75,

    Welcome to MSDN Forum.

    An ObjectEntry object has a Entity property, you can get the actual entity from this property. Below is a demo.

    static void Main(string[] args)
            {
                using (BreakAwayEntities context = new BreakAwayEntities())
                {
                    Customer cus = context.Customers.First();
                    ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(cus);
                    Customer c = (Customer)entry.Entity;
                }
            }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 9:16 AM
  • How can I pass the entity type to the generic method GetTableName because context.CreateObjectSet is also a generic method.

    Alternatively, is it possible to get the table name without using the generic method above?

    Thursday, May 31, 2012 9:21 AM
  • Hi Cryo75,

    Sorry, I still not clear about the problem. My understanding is, now you have an ObjectStateEntry instance, you want to get the actual entity of this entry, as the title of the case, and then pass the type of this entity to 'GetTableName' method. If I understand correctly, the code I posted in the previous reply can help you to achieve it. If I understand incorrectly, please feel free to correct me. Furthermore, if your EntitySet's name is as same as the database table's name, you can also get the table's name through ObjectStateEntry's EntitySet property.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 9:33 AM
  • The EntitySet's name is different from the db name. In the your code above, I cannot use this line:

    Customer c = (Customer)entry.Entity;

    Because as I'm auditing any kind of entity during saving, I cannot know what type it is, hence casting won't working in this situation.

    Thursday, May 31, 2012 9:39 AM
  • Hi Cryo75,

    OK, I will do more research on this issue and come back as soon as possible. : )

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 10:05 AM
  • Hi Cryo75,

    I have tried to use reflection to realize it, please refer to the code below, I can get the table name in my test.

    namespace TestConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (NorthwindEntities context = new NorthwindEntities())
                {               
                    Customer cus = context.Customers.First();
    
                    ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(cus);
                    Type t = entry.Entity.GetType();
                    MethodInfo method = typeof(ExtensionMethod).GetMethod("GetTableName");
                    MethodInfo genericMethod = method.MakeGenericMethod(t);
                    string table = genericMethod.Invoke(null, new object[]{context}).ToString();
                    Console.WriteLine(table);
                    Console.Read();
                }
            }
        }
    
        public static class ExtensionMethod
        {
            public static string GetTableName<T>(this ObjectContext context) where T : class
            {
                string sql = context.CreateObjectSet<T>().ToTraceString();
                Regex regex = new Regex("FROM (?<table>.*) AS");
                Match match = regex.Match(sql);
    
                string table = match.Groups["table"].Value;
                return table;
    
            }
        } 
    
    }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cryo75 Wednesday, June 6, 2012 8:05 AM
    Sunday, June 3, 2012 6:11 AM
  • The code works. One small thing.. is there a way to get just the table name (eg. customers) instead of the whole schema (eg. [dbo].[customers]??

    Thanks,

    Ivan

    Tuesday, June 5, 2012 1:43 PM
  • Hi Cryo75,

    I think it could achieve by modify the regular expression, but I'm afraind I'm not familar with it. Please try to get help from Regular Expression Forum.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 6, 2012 8:05 AM
  • Problem has been solved :)
    Wednesday, June 6, 2012 8:05 AM