7

We created a web application using the model first approach. A new developer came into the project and created a new custom model using the code first approach (using a database file). The

Here is the code first database context.

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 throws an error Could not find the conceptual model type for 'WVITDB.Models.FavoriteProject'. and refers to the original database model. We are not sure why (or how) it is calling that.

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

When the DashboardContextclass is instantiated the error are shows up for both of the DBset properties.

Is there are a reason why the controller calling the wrong database?

EDIT:

FavoriteProject is in a different context (our main data model) and not related to the new custom model.

1
  • i'd 'have a word' with the new developer and get him/her to prove that the two can happily live together via an offline proof of concept and then get them to integrate it into a shelveset before putting it into your core base. sounds very dodgy approach as is.. you can however, 'attach' non codefirst tables programatically.. will jot the syntax in follw-up message. Commented Aug 1, 2011 at 14:13

2 Answers 2

3

Found an answer, it maybe not what you want to hear though:

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

"If you are using the default code generation for the EDMX file then the generated classes contain a series of attributes to help EF find which class to use for each entity type. EF currently has a restriction that POCO classes can't be loaded from an assembly that contains classes with the EF attributes. (Short answer is no, your classes need to be in separate projects).

This is a somewhat artificial restriction and something that we realize is painful and will try and remove in the future."

So the workaround would be to split the classes into two different assemblies.

Sign up to request clarification or add additional context in comments.

Comments

0

ajpaz - you may have to 'map' your existing model to the database table programatically. i'm assuming from the error message that its looking for the FavouriteProject table/class mapping. maybe the db has this defined as a singular, in which case try:

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

you'd also need to do a dbset as per above (or some permutation of plurals):

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

might be miles off, just a thought

[edit] - overiding your code 1st dbcontext in web.config (under connectionstrings [name MUST match 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"/> 

6 Comments

The problem is FavouriteProject has nothing to do with this particular dbContext. The FavouriteProject is not called in the controller, model, or view. The DashboardContext and FavoriteProject are not related.
so, you dont reference a collection or parent property of FavouriteProject from inside any of the 2 code first classes... hmm, strange error methinks. the legacy model doesn't have an FK onto any of these new models either i presume
I'm on the team too... so to chime in. The data models are completely separate and don't reference each other. The newer data model was actually built and tested in it's own project. Once we merged the new models/views/controllers into the existing application, we encountered this error.
ZeroDivide - could it be an issue with the namespaces?? might it be better to keep the two (dal/model) assemblies seperate until you get the controllers and views working against each respective source?? also, you could try overriding the dbcontext connectionstring inside your web.config so that you know for certain where its pointing to
Initially the models were in separate namespaces. We actually tried combining them into the same namespace and it was still the same error. I think overriding the dbcontext connectionstring would be a good approach. Could you explain where and how to do this. Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.