Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Entity type <T> is not part of the model for the current context

I have tested the other tables in the model and they are working. However, I added two new tables and they are not in the model.

How do I ensure that a database table is part of the model?

OnModelCreating is NOT called in this code. enter image description here

The entity type AppToLeadRequestLine is not part of the model for the current context.

This exception is thrown when adding an item to the table.

public void Add<T>(T obj) where T : class
{
   Set<T>().Add(obj);
}

I added both of these tables to the database.

enter image description here

After adding these two tables to the database I generated them in the model by using Update Model from Database, and both tables appear in the model. enter image description here

When I look at the table mapping they appear to be mapped to the correct POCO. I'm not 100% sure about this.

enter image description hereenter image description here

The classes look like this: enter image description hereenter image description here

like image 275
Jonathan Kittell Avatar asked Nov 10 '22 16:11

Jonathan Kittell


1 Answers

OnModelCreating is not called -- as you pointed out -- so I can be certain that you are using a Model-First approach (and not Code-First). In this case, you are responsible for all of the mapping. Here is a good article on the topic.

It appears that RequestId is a foreign key to the AppToLeadRequest class -- if this is the case, you must declare the public virtual statements in both classes yourself.

For example -- at the bottom of the AppToLeadRequest class, it should contain:

public virtual ICollection<AppToLeadRequestLine> AppToLeadRequestLines { get; set; }

and, at the bottom of the AppToLeadRequestLine class, it should contain:

public virtual AppToLeadRequest AppToLeadRequest { get; set; }

I would also change RequestId to AppToLeadRequestId to follow a <class name>Id format.

like image 167
Bret Avatar answered Nov 15 '22 06:11

Bret