Mapping to a nested class
I am getting the following error when my application runs:
开发者_如何学PythonSystem.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.
The wrong part is that you are trying to map nested class. It is not supported by entity framework.
I don't know what is wrong with your code but if you want to start with a database and generate the C# code use the entity framework power tools CTP1 http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx
精彩评论