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.
Leave a Reply