Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:50:15+00:00 2026-06-15T05:50:15+00:00

I’m using Table per Hierarchy (TPH). For example we have a base class for

  • 0

I’m using Table per Hierarchy (TPH).
For example we have a base class for all entities:

public abstract class Entity
    {
        public virtual int Id { get; set; }

        public virtual bool IsTransient()
        {
            return Id == default(int);
        }
    }

And base class for several entitites:

public abstract class Event:Entity
    {
        [MaxLength(50)]
        [Required]
        public string Name { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        [MaxLength(100)]
        public string ShortDescription { get; set; }

        [Required]
        public DateTime PublishDate { get; set; }

        public int  Duration { get; set; }
    }

public class Film:Event
    {
        public string Director { get; set; }

        public string ActorList { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

public class Concert:Event
    {
        public string Genre { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

My context:

public class MyContext:DbContext
    {
        public MyContext():base(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString)
        {
        }

        public DbSet<Event> Events { get; set; }

        public virtual void Commit()
        {
            base.SaveChanges();
        }

    }

This is base repository:

public class GenericRepository : IRepository
{
  //...   

    public IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class
            {
                return GetQuery<TEntity>().AsEnumerable();
            } 

    public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
            {          
                var entityName = GetEntityName<TEntity>();
                return ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
            }

private string GetEntityName<TEntity>() where TEntity : class
        {
            string entitySetName = ((IObjectContextAdapter)DbContext).ObjectContext
                 .MetadataWorkspace
                 .GetEntityContainer(((IObjectContextAdapter)DbContext).ObjectContext.DefaultContainerName, DataSpace.CSpace)  
                 .BaseEntitySets.First(bes => bes.ElementType.Name == typeof(TEntity).Name).Name;

            return string.Format("{0}.{1}", ((IObjectContextAdapter)DbContext).ObjectContext.DefaultContainerName, entitySetName);
        }

    }

Next, create context and repository:

var context = new MyContext();
EventRepository repository = new EventRepository(context);
var films = repository.GetAll<Film>();

But I get exception (in the GetEntityName method): the sequence does not have elements.
I think it because there are no Film table in the DB. How to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:50:16+00:00Added an answer on June 15, 2026 at 5:50 am

    I don’t see the need of GetEntityName in the repository you are showing. For GetQuery you can use the DbContext API directly and don’t need to access the underlying ObjectContext or MetadataWorkspace:

    public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
    {          
        return DbContext.Set<TEntity>();
    }
    

    This returns a DbSet<TEntity> (which is an IQueryable<TEntity>). I am not 100% sure if that also works if TEntity is derived but the MSDN documentation about DbSet<TEntity> says: “The type can be derived type as well as base type.” So, I would hope that the Set<TEntity>() method is allowed for derived types as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.