Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping to a nested class

I am getting the following error when my application runs:

System.InvalidOperationException: The type 'ContactModels+Contact' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

It is failing when my DBContext class tries to initialize the entities:

public class DB : DbContext
{
    public DbSet<ContactModels.Contact> Contacts { get; set; }
    ....
}

The Contact model is as follows:

public class ContactModels
{
    public class Contact
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        ...
    }
}

Connection string:

<add name="DB" connectionString="Data Source=XXXXX;Initial Catalog=XXXX;Trusted_Connection=True"
  providerName="System.Data.SqlClient" />

I get the error whether the database exists or even if it does not exist and I have it being initialized:

protected void Application_Start()
{
    Database.SetInitializer(new CreateDatabaseIfNotExists<Models.DB>());
    ....
}

This is my first time using EF, I've followed a few tutorials but I am using SQL Server 2008 R2 and would prefer to have the database created myself rather than have EF create it for me. Though at this point I'll take either if it works.

like image 842
Terry Avatar asked Dec 29 '25 02:12

Terry


1 Answers

The wrong part is that you are trying to map nested class. It is not supported by entity framework.

like image 149
Ladislav Mrnka Avatar answered Dec 30 '25 17:12

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!