实体框架映射异常

本文关键字:异常 映射 框架 实体 | 更新日期: 2023-09-27 18:14:34

我正在学习实体框架,所以我创建了一个简单的模型与两个表,添加适当的类,我试图写一个简单的repo,但应用程序粉碎在我的repo:(

MyEntityPOCO是我的控制台应用程序项目的名称:)

数据库可视化链接http://wstaw.org/w/LrL/

这是我的repo代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Objects;
    namespace MyEntityPOCO
    {
         public class Entities : ObjectContext
         {
              private ObjectSet _contacts;
              private ObjectSet _addresses;
              public Entities()
                   : base("name=MyEntities", "MyEntities")
              {
                   _contacts = CreateObjectSet();
                   _addresses = CreateObjectSet();
              }
              public ObjectSet Contacts
              { get { return _contacts; } }

              public ObjectSet Addresses
              { get { return _addresses; } }
         }
    }


这是关于异常的详细信息。


    {"Mapping and metadata information could not
     be found for EntityType 'MyEntityPOCO.Contact'."}

This is Contact


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
        namespace MyEntityPOCO
        {
             public class Contact
             {
                  public int ContactID { get; set; }
                  public string FirstName { get; set; }
                  public string LastName { get; set; }
                  public ICollection Addresses { get; set; }
             }
        }

地址

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace MyEntityPOCO
    {
         public class Address
         {
              public int AddressID { get; set; }
              public string Street { get; set; }
              public string City { get; set; }
         }
    }

This my App.Config


    
    
      
      
    

这是来自模型属性的连接字符串


    metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=COMPAL'COMPALSERWER;initial catalog=MyBase1;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"

这是来自服务器资源管理器的连接字符串


    Data Source=COMPAL'COMPALSERWER;Initial Catalog=MyBase1;Integrated Security=True

和来自服务器资源管理器的提供者

.NET Framework Data Provider for SQL Server

实体框架映射异常

您的联系人的'地址'是一个集合…?你必须声明它是一个Address对象的集合,这样EF才能正确地进行映射。

public class Contact
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<Address> Addresses { get; set; }
}