EF Designer Database first forces mapping to plural named tables

290 Views Asked by At

I am doing Entity Frameworks Database first approach, using EF 5.0, visual studio 2012 and Sql server management studio. Both Vs and ssms are express version.

I have several tables named ex. "User" or "Product", but when autogenerating the table mapping between an entity and a table, EF insists on mapping "User" to dbo.Users and "Product" to dbo.Products.

I have tried setting tools->options->databasedesigner->O/R designer->Pluralization of names to false.

When working with the *.edmx file in the EF designer in VS2012, I have also tried to set "Pluralize New Objects" to false.

In the EF Designer it correctly shows that "User" maps to the "User" table, but when I inspect my dbcontext object in debugging mode during runtime and finds the sql it uses to access the tables it access dbo.Users.

If I were using Code first approach this seems to be fixable by "modelBuilder.Conventions.Remove();" in the ModelBuilder, but how is this problem solved using the Database first approach?

I should probably say that I use a IDbcontext for testing purpose

using System.Data.Entity.Infrastructure;

namespace XX.Core
{
    public interface IDbContext : IDisposable
    {
        IQueryable<T> Query<T>() where T : class;

        void Add<T>(T entity) where T : class;

        void Delete<T>(T entity) where T : class;

        void Update<T>(T entity) where T : class;

        Boolean Any<T>() where T : class;

        int SaveChanges();

        void Dispose();
    }
}

and my actual dbcontext when not testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace XX.Core.Model
{
    public class XXDb : DbContext, IDbContext
    {
        public XXDb()
            : base("name=XX")
        {

        }

        public DbSet<User> Users { get; set; }
        public DbSet<YY> Products { get; set; }
        public DbSet<Offer> YY { get; set; }
        public DbSet<ZZ> ZZ { get; set; }

        IQueryable<T> IDbContext.Query<T>()
        {
            return Set<T>();
        }

        void IDbContext.Add<T>(T entity) 
        {
            Set<T>().Add(entity);
        }

        void IDbContext.Delete<T>(T entity) 
        {
            Set<T>().Remove(entity);
        }

        void IDbContext.Update<T>(T entity)        
        {
            Set<T>().Attach(entity); 

            SaveChanges();
        }

        Boolean IDbContext.Any<T>()
        {
            return Set<T>().Any();
        }
    }
}
0

There are 0 best solutions below