映射到一个嵌套类嵌套

2023-09-03 20:30:05 作者:清酒

我收到以下错误我的应用程序运行时:

I am getting the following error when my application runs:

System.InvalidOperationException:类型   ContactModels +联系方式并没有映射。检查   类型还没有被明确排除通过使用忽略方法或   NotMappedAttribute数据注解。验证类型定义   作为一个阶级,是不是原始的,嵌套或通用的,不继承   从EntityObject。

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.

这是失败的时候,我的DbContext类尝试初始化实体:

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>());
    ....
}

这是我第一次使用EF,我跟着一些教程,但我使用SQL Server 2008 R2和将preFER有数据库中创建自己,而不是让EF创建我。虽然在这一点上我不是坐,如果它的工作原理。

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.