Cannot create a DbSet for "MyModal" because this type is not included in the model for the context. However the model contains an entity type with the same name in a different namespace


Question: I have no idea what is going on with this error. Yes, the error is quite descriptive but finding exactly where the issue might be is challenging. See the error below:

"Cannot create a DbSet for "MyModal" because this type is not included in the model for the context. However, the model contains an entity type with the same name in a different namespace".

What am I missing?


Login to See the Rest of the Answer

Answer:
This is most likely the Runtime Error, you don't see this error when Building or cleaning the Solution but when you attempt to run the application. This is called a Runtime Error.

From the description of the error, it sounds like you have a DbSet for your Modal in the Database Context C-sharp class but you haven't defined that Modal in the OnCreate Section of the Database Context.

See possible solution below:

1. Verify that you are not using the same namespace in two (which is supposed to be two different classes) the same name of the Modal class. If yes, rename the NameSpace in the other Model Class.
2. Make sure that the Model Class that is throwing an error is not trying to use the wrong NameSpace somewhere in your code.

     A case for this issue is when you have a UserProfile Model class that you copied from another NameSpace and try to use it in a new Project but somewhere in your new project, you are still referencing the Old NameSpace. This causes the conflict on Runtime and the Compiler doesn't know which Class from which NameSpace to register into the Assembly.







Ken said:

Take a look at your ApplicationDbContext class, chances are that you have used a different "MyModal" class from a different Namespace.

Posted On: February 07, 2022 17:11:31 PM
Jack said:

In my case it was the naming of the DbSet property, the property name had an "s" in it while the Compiler was looking for a none plural version of the name.

Posted On: February 07, 2022 11:19:17 AM

© 2024 - ErnesTech - Privacy
E-Commerce Return Policy