0

I am using the code first approach with a local database (localdb\11.0v) in ASP MVC 4. I have created my entity

public class Service
    {
        public int ServiceID { get; set; }
        public string ServiceName { get; set; }
    }

My DbContext

public class EFDbContext : DbContext
    {
        public DbSet<Service> Service { get; set; }
    }

My repositories

public interface IServiceRepository
    {
        IQueryable<Service> Service { get; }
    }

Interface

public class EFServiceRepository : IServiceRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Service> Service
        {
            get { return context.Service; }
        }
    }

I am using Ninject for the binding

 ninjectKernel.Bind<IServiceRepository>().To<EFServiceRepository>();

With the controller referencing the ninject kernel bind

public ActionResult List()
        {
            return View(repo.Service);
        }

So this all works which is good but it does not work the way that i want it to. It pulls a list from the table "Services" but i want it to pull data from the table "Service". I dont understand why this is happening. I did have the var names plural so they were "Services" but that should not make a difference ( should it? ) I changed them all to "Service" in hope that would solve it. I really feel like i was understanding all this until now.

We have "business" rules for the db and they need to be singular names + i really want to understand why it is using the "Services" table and not the "Service" table. I only added the "Services" table to test.

2 Answers 2

6

First you need to add this code into "Using" Blocks of EFDbContext class:

using System.Data.Entity.ModelConfiguration.Conventions

Then Add this code into EFDbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi , thanks for the quick reply. Is ModelBuilder part of a reference i should be using. Or should i create a class for it ( sorry if this is a noob question , having been doing this for to long )
Not really. I have add the code and I get a reference for the <PluraluzingTableNAmeConvention> But i am not sure about the (ModelBuilder modelBuilder) it says
"The type of namespace name ModelBuilder could not be found, are you missing an assembly refernece
0

There are two different ways to override Entity Framework's naming conventions wrt table names:

First is through data annotations on the entity class:

[System.ComponentModel.DataAnnotations.Schema.Table("Service")]
public class Service
{
    ...

Second is through fluent mapping in the context class itself:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Service>().ToTable("Service");

2 Comments

So the naming convention is to hit plural tables even if you have defined the table name in the code? this seems really old
Well, you don't define tables names in code, just class names. To translate them to plural tables is just a convention (as Bappi Datta shows).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.