Mapping to a nested class

早过忘川 提交于 2019-12-07 14:42:25

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/7583996/mapping-to-a-nested-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!