IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact
You are here: Home / .NET / Map Model Classes to Differently Named Tables in Entity Framework

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

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

Recent Posts

  • Gotcha in Executing SSIS from DTExec Command Line
  • How to Create Kofax TotalAgility (KTA) SDK API in ASP.NET
  • How to Resolve ReferenceManagerPackage Error in Visual Studio 2019
  • F#: Seq, List, Array, Map, Set. Which One to Use?
  • How to Get Table Definition in SQL Server
WP Engine Power your Brand with Managed WordPress

Recent Posts

  • Gotcha in Executing SSIS from DTExec Command Line
  • How to Create Kofax TotalAgility (KTA) SDK API in ASP.NET
  • How to Resolve ReferenceManagerPackage Error in Visual Studio 2019
  • F#: Seq, List, Array, Map, Set. Which One to Use?
  • How to Get Table Definition in SQL Server
  • RSS

Tags

.NET Core Access Adobe AdSense Amazon ASP.NET Cdonts Dll Classic ASP Code Editor Connect-It ETL FSharp Genesis Framework Git Google HP Asset Manager HTML HTML5 Hugo IIS Information Security Internet Internet Information Services iOS Linux macOS Microsoft Microsoft SQL Server MVC PHP Simple Mail Transfer Protocol Smtp Server Social Media SQL SQL Server SSIS SSMS SSRS Visual Studio VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

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