Shuffle the model first and code first

We created a web application using the first model approach. A new developer entered the project and created a new user model using the first code approach (using the database file).

Here is the context of the first code database.

namespace WVITDB.DAL { public class DashboardContext : DbContext { public DbSet<CTOReview> CTOReviews { get; set; } public DbSet<Concept> Concepts { get; set; } //public DashboardContext() // : base("name=DashboardContext") //{ //} // protected override void OnModelCreating(DbModelBuilder modelBuilder) // { // //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); // } } } 

The following controller method generates an error: Could not find the conceptual model type for 'WVITDB.Models.FavoriteProject'. and refers to the original database model. We do not know why (or how) this is called.

  public ViewResult Index() { var d = db.Concepts.ToList(); //Throws error here return View("Index",d); } 

When creating an instance of DashboardContextclass, an error occurs for both DBset properties.

Is there a reason the controller is causing the wrong database?

EDIT:

FavoriteProject is in a different context (our main data model) and does not apply to the new user model.

+7
source share
2 answers

Found the answer, maybe this is not what you want to hear:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d2a07542-cb33-48ba-86ed-4fdc70fb3f1a

“If you use the default code generation for an EDMX file, then the generated classes contain a number of attributes to help EF find which class to use for each type of entity. Currently, EF has a limitation that POCO classes cannot be loaded from an assembly containing classes with EF attributes (the short answer is no, your classes should be in separate projects).

This is a somewhat artificial limitation, and something that we understand is painful and will try to remove in the future. "

So, a workaround would be to break the classes into two different assemblies.

+3
source

ajpaz - you may need to "map" your existing model to a database table programmatically. I assume from the error message that it is looking for a FavoriteProject table / class mapping. perhaps db has it defined as the only one, in which case try:

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

you also need to do dbset as above (or some plural permutation):

 public DbSet<FavouriteProjects> FavouriteProjects{ get; set; } 

maybe a few miles just a thought

[edit] - redefinition of the 1st dbcontext code in web.config (in the connectionstrings [name MUST section matches your dbcontext name]):

 <add name="DashboardContext" connectionString="Data Source=remote.sqlserver.server;Initial Catalog=code_first_db;Persist Security Info=True;MultipleActiveResultSets=True;User ID=code_first_db_user;Password=password" providerName="System.Data.SqlClient"/> 
0
source

All Articles