locked
context.AddObject(string EntitySetName, object entity) cannot find EntitySetName RRS feed

  • Question

  • Hi,

    I am trying to use "context.AddObject(string EntitySetName, object entity)" but I get an error saying that the EntitySetName could not be found. On investigating further I found that this was because the entity set name I was looking for was not present in the list of BaseEntitySets. How do I find the BaseEntitySet name from the DbSet name?

    Ultimately I am simply trying to create a generic method to add an entity to a context without knowing the type of the DbSet at compile time. I can get the name of the DbSet (or even its type) but I cannot use DbSet<T> because T is unknown. I have this so far:

    DbContext dBContext = Activator.CreateInstance(contextType);

    ObjectContext = ((IObjectContextAdapter_dBContext).ObjectContext;

    var entity = Activator.CreateInstance(entityType);

    oBContext.AddObject("EntitySetName", entity);

    but because the EntitySetName does not match the BaseEntitySet name the AddObject fails. The EntitySetName  does not match the BaseEntitySet name because the EntitySet is derived from another class, e.g.

    Donor : Person

    To complicate matters further EF then pluralizes "Person" to "People" for the BaseEntitySet. I switched off the pluralization convention but this only prevents pluralization of table names, not DbSet names.

    Thanks


    ChangedDaily




    • Edited by ChangedDaily Friday, December 21, 2012 4:41 PM Updated title
    Friday, December 21, 2012 12:48 PM

Answers

  • Hi, after quite a bit of investigation and testing I have come up with a method of digging up the information I needed:

                EntityContainer container = oBContext.MetadataWorkspace.GetEntityContainer
                                    (oBContext.DefaultContainerName, DataSpace.CSpace);
    
                Dictionary<string, string> entityBaseNameBaseEntitySet =
    
                            (from e in container.BaseEntitySets
                             where e is EntitySet
                             select e).ToDictionary(e => e.ElementType.Name, e => e.Name);
    
                Dictionary<string, EntitySetAndTypeInfo> entitySetInfo =
                    
                            (from p in t.GetProperties()
                             where p.PropertyType.IsGenericType &&
                             (p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>) ||
                              p.PropertyType.GetGenericTypeDefinition() == typeof(IDbSet<>))
                             select new EntitySetAndTypeInfo()
                             {
                                 EntitySetName = p.Name,
                                 EntityType = p.PropertyType.GetGenericArguments().FirstOrDefault(),
                                 EntityBaseType = FindEntityBase(p.PropertyType.GetGenericArguments().FirstOrDefault()),
                                 BaseEntitySet =
                                    (from c in container.BaseEntitySets
                                    where c is EntitySet
                                    where c.Name == FindEntityBase(p.PropertyType.GetGenericArguments().FirstOrDefault())
                                    select c.ElementType.Name).FirstOrDefault()
                             }).ToDictionary(d => d.EntitySetName, d => d);

        public class EntitySetAndTypeInfo
        {
            public string EntitySetName { get; set; }
            public Type EntityType { get; set; }
            public string EntityBaseType { get; set; }
            public string BaseEntitySet { get; set; }
        }


    ChangedDaily

    • Marked as answer by ChangedDaily Sunday, December 30, 2012 6:01 PM
    Sunday, December 30, 2012 6:00 PM

All replies

  • Did you create code first or database first maps?

    JP Cowboy Coders Unite!

    Friday, December 21, 2012 1:39 PM
  • Hi, I am using code first.


    ChangedDaily

    Friday, December 21, 2012 3:06 PM
  • From a support perspective this is really beyond what we can do here in the forums. If you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs:  http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone.
    • Proposed as answer by Mark Ba - MSFT Thursday, December 27, 2012 5:14 PM
    Thursday, December 27, 2012 5:14 PM
  • Hi, after quite a bit of investigation and testing I have come up with a method of digging up the information I needed:

                EntityContainer container = oBContext.MetadataWorkspace.GetEntityContainer
                                    (oBContext.DefaultContainerName, DataSpace.CSpace);
    
                Dictionary<string, string> entityBaseNameBaseEntitySet =
    
                            (from e in container.BaseEntitySets
                             where e is EntitySet
                             select e).ToDictionary(e => e.ElementType.Name, e => e.Name);
    
                Dictionary<string, EntitySetAndTypeInfo> entitySetInfo =
                    
                            (from p in t.GetProperties()
                             where p.PropertyType.IsGenericType &&
                             (p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>) ||
                              p.PropertyType.GetGenericTypeDefinition() == typeof(IDbSet<>))
                             select new EntitySetAndTypeInfo()
                             {
                                 EntitySetName = p.Name,
                                 EntityType = p.PropertyType.GetGenericArguments().FirstOrDefault(),
                                 EntityBaseType = FindEntityBase(p.PropertyType.GetGenericArguments().FirstOrDefault()),
                                 BaseEntitySet =
                                    (from c in container.BaseEntitySets
                                    where c is EntitySet
                                    where c.Name == FindEntityBase(p.PropertyType.GetGenericArguments().FirstOrDefault())
                                    select c.ElementType.Name).FirstOrDefault()
                             }).ToDictionary(d => d.EntitySetName, d => d);

        public class EntitySetAndTypeInfo
        {
            public string EntitySetName { get; set; }
            public Type EntityType { get; set; }
            public string EntityBaseType { get; set; }
            public string BaseEntitySet { get; set; }
        }


    ChangedDaily

    • Marked as answer by ChangedDaily Sunday, December 30, 2012 6:01 PM
    Sunday, December 30, 2012 6:00 PM