IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

Map Model Classes to Differently Named Tables in Entity Framework

For a few days, I kept running into this minor but annoying issue using EF 4.2 with MVC3 where the web application refused to map a class (Profile) to an existing table (tblProfile) in a database [CPDB] and instead created a brand new table with the same name on a different database [CPDB.Domain.Entities.ProfileEntities].

What confused me even more was I did this step before for another class (CarAssignment) and it didn’t cause any problems. It took me a while before I figured out the small difference on how I setup the two entities.

CarAssignment.cs

namespace CPDB.Domain.Entities
{
   [Table("tblCar")]
   public class CarAssignment
   {
      [Key]
      public int CarId { get; set; }
      [StringLength(25)]
      public string CompanyType { get; set; }
      [...]
   }
}

Profile.cs

namespace CPDB.Domain.Entities
{
   [Table("tblProfile")]
   public class Profile
   {
      [Key]
      public int ProfileId { get; set; }
      [Required]
      [StringLength(30), Display(Name = "First name")]
      public string FirstName { get; set; }
      [...]

SqlCarAssignmentRepository.cs

namespace CPDB.Domain.Concrete
{
   public class SqlCarAssignmentsRepository
       : ICarAssignmentsRepository
   {
      private CarEntities db = new CarEntities();
      [...]

SqlProfileRepository.cs

namespace CPDB.Domain.Concrete
{
   public class SqlProfilesRepository : IProfilesRepository
   {
      private ProfileEntities db = new ProfileEntities();

      private IQueryable<Profile> ProfilesTable;
      [...]

NinjectControllerFactory.cs

namespace CPDB.WebUI.Infrastructure
{
   public class NinjectControllerFactory : DefaultControllerFactory
   {
      private IKernel kernel =
          new StandardKernel(new AMPServices());

      protected override IController GetControllerInstance(
          RequestContext requestContext,
          Type controllerType)
      {
         if (controllerType == null)
            return null;
         return (IController)kernel.Get(controllerType);
      }

      private class AMPServices : NinjectModule
      {
         public override void Load()
         {
            Bind()
            .To()
            .WithConstructorArgument("connectionString",
            ConfigurationManager.ConnectionStrings["CarEntities"]
            .ConnectionString);

            Bind()
            .To()
            .WithConstructorArgument("connectionStrings",
            ConfigurationManager.ConnectionStrings["DifferentName"]
            .ConnectionString);
         }
      }
   }
}

ConnectionStrings in Web.config file:

<connectionStrings>
  <add name="DifferentName" connectionString="Server=.SQLEXPRESS;
       Database=CPDB;Trusted_Connection=yes"
       providerName="System.Data.SqlClient" />
</connectionStrings>

If we just stop here, the web application will create a new table dbo.tblProfile in a new database. In order to use an existing table and database, I had to add line 8-10 on ProfileEntities class.

CarEntities.cs

namespace CPDB.Domain.Entities
{
   public class CarEntities : DbContext
   {
      public DbSet<CarAssignment> CarAssignments { get; set; }
   }
}

ProfileEntities.cs

namespace CPDB.Domain.Entities
{
   public class ProfileEntities : DbContext
   {
      public ProfileEntities()
         : base("DifferentName")
      { }

      public DbSet<Profile> Profiles { get; set; }
   }
}

If you want to use a different name, you need to alter the constructor on the DbContext. You don’t have to necessarily specify the physical database name, instead, you can use your connection strings parameter in the Web.Config file.

November 20, 2011 Filed Under: .NET, How To Tagged With: Internet, MVC

Buy me a coffee?

Buy me a coffee If you find any of the articles or demos helpful, please consider supporting my work here, you'll have my big thanks!

Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2025 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure