繁体   English   中英

实体框架映射异常

[英]Entity framework mapping exception

我正在学习Entity Framework,所以我创建了一个带有两个表的简单模型,添加了适当的类,并尝试编写一个简单的存储库,但是应用程序在我的存储库中崩溃了:(

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

链接到数据库虚拟化http://wstaw.org/w/LrL/

这是我的回购代码



    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'."}

这是联系方式



    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; }
         }
    }

这是我的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

您联系人的“地址”是...的集合? 您必须声明它是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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM