单个Asp.Net MVC应用程序中的多个.edmx

时间:2016-10-21 16:25:09

标签: asp.net-mvc entity-framework asp.net-mvc-4 metadata edmx

我在ASP.Net MVC中创建一个网站,我需要访问2个不同的数据库。我目前在web.config文件中使用2个.edmx文件,每个文件都有自己的连接字符串。我有一个元数据文件,用于描述具有必要属性的不同表。我的问题是在控制器中我试图使用db'但我一直收到这个错误:

The entity type <object2Metadata> is not part of the model for the current context.  

我已经尝试了所有我能想到的东西,例如使用var并且只在必要时专门键入我的对象,并且在我的代码的不同步骤中会出现错误,无论我多么努力地做到正确。< / p>

我想要实现的目标:我在两个不同的数据库中有两个表,我需要比较他们的电子邮件并获取一个表中但不存在于另一个表中的所有电子邮件帐户,以及把那些相应的对象放在一个列表中。

我在项目中创建了元数据,将元素映射到对象中。对象不是相同的,并且不能被映射为彼此相等,因为object2中的属性不存在于object1中且没有相应的字段,所以我可以&#39;只需将object2转换为object1并进行比较。我尝试从每个对象中取出电子邮件,然后将它们放在各自的列表中,然后比较这些电子邮件并获得差异。这是有效的,然而当我再次访问db2以提取具有这些电子邮件的对象时,我再次得到该错误。当我开始尝试使用第二个数据库时,我似乎只得到错误。我检查了我的连接字符串,它们是正确的。它允许我从db2中取出初始对象,将它们放入var..so我知道连接字符串可以正常工作,但是当我尝试从db2操作数据时似乎发生了错误...如果这是有意义的。

我完成了我的研究,但找不到能解决我具体问题的任何内容。 例如:Multiple edmx in one projectHow to create multiple edmx in same project

以下是我一直在做的一些代码示例: 控制器 -

namespace project.Controllers
{
    public class myController: Controller
    {
      //both these statements compute just fine with no errors.
      private db1Entities db = new db1Entities();
      private db2Entities adb = new db2Entities ();

        // GET: Exchange
        public ActionResult Index(int? page)
        {
          //Getting my initial lists from the db's
          //Both these statements compute with no errors as well
          var mbe = db.table1.AsEnumerable().ToList();
          var abe = adb.table2.AsEnumerable().ToList();

          //This list would be used for me to convert Obj2 into Obj1
          List<object1> abeConverted = new List<object1>();

          //This list would then hold the items in List<obj2> that aren't
          //in List<obj1>
          List<object1> abeDiff = new List<object1>();

         ////Here's where I was going to try to just filter by email
         //List<string> mbeEmails = new List<string>();
         //List<string> abeEmails = new List<string>();            

        //Here is where I actually converted obj2 into obj1-no error here
        foreach (object2 a in abe)
        {
            object1 m = new object1 ();
            m.pk_email = a.mail;

            if (a.secureMail.Equals('F'))
                m.isSecure = false;
            else
                m.isSecure = true;

            m.jobCode = null;
            m.workOrder = null;
            m.isDisabled = false;
            m.isNonBillable = false;
            m.createdDate = a.createdDate;

            abeConverted.Add(m);
        }

        //Here's where I would have extracted emails to lists from obj's
        //foreach (var m in mbe)
        //{
        //    string e = "";
        //    e = m.pk_email;
        //    mbeEmails.Add(e);
        //}

        //foreach (var a in abe)
        //{
        //    string e = "";
        //    e = a.mail;
        //    abeEmails.Add(e);
        //}

         ////Email lists compare to get differences             
        //List<string> diffEmails = (from a in abeEmails
        //                    join m in mbeEmails on a equals m into d
        //                    from od in d.DefaultIfEmpty()
        //                    where od == null
        //                    select a).ToList<string>();

        //compare to get differecnes by objects after the obj2 convert
        //Here is where the error pops up by using this approach.
        abeDiff = (from a in abeConverted
                   join m in mbe on a.pk_email equals m.pk_email into d
                   from od in d.DefaultIfEmpty()
                   where od == null
                   select a).ToList();

        //This would be used if needed to get the objects after the email
        //compare.  Here is where the error pops up using this filtered 
        //emails approach.
        //foreach (string e in diffEmails)
        //{
        //    object2 a = new object2 ();
        //    a = abe.Where(b => b.mail == e).FirstOrDefault();
        //    abeDiff.Add(a);
        //}

        //Turning my list into a pagedList object for the view
        int pageSize = 20;
        int pageNumber = (page ?? 1);

        var abePage = abeDiff.OrderBy(a => 
             a.pk_email).ToPagedList(pageNumber, pageSize);

        ViewBag.abePage = abePage;
        return View(abeDiff.OrderBy(a => 
             a.pk_email).ToPagedList(pageNumber, pageSize));

        }
    }
}  //end Controller

这是我的两个对象的MetaData -

namespace project.Models
{
  public class Metadata
  {
  }

  public class object1Metadata
  {
    [Display(Name = "Email")]
    [Required]
    public string pk_email { get; set; }

    [Display(Name = "Job Code")]
    [Required]
    public string jobCode { get; set; }

    [Display(Name = "Work Order")]
    [Required]
    public string workOrder { get; set; }

    [Display(Name = "Secure?")]
    [Required]
    public bool isSecure { get; set; }

    [Display(Name = "Disabled?")]
    public bool isDisabled { get; set; }

    [Display(Name = "Non-Billable?")]
    [Required]
    public bool isNonBillable { get; set; }

    [Display(Name = "Date Created")]
    [Required]
    public DateTime createdDate { get; set; }
  }

  public class object2Metadata
  {
    //[Display(Name = "ID")]
    //public double Id { get; set; }

    [Display(Name = "Department Number")]
    public string departmentNumber { get; set; }

    [Display(Name = "Division")]
    public string division { get; set; }

    [Display(Name = "Agency")]
    public string agency { get; set; }

    [Display(Name = "Department Name")]
    public string departmentName { get; set; }

    [Display(Name = "Name")]
    public string name { get; set; }

    [Display(Name = "Email")]
    public string mail { get; set; }

    [Display(Name = "Size")]
    public string mbSize { get; set; }

    [Display(Name = "Distinguished Name")]
    public string distinguishedName { get; set; }

    [Display(Name = "Instant Messaging")]
    public string instantMessaging { get; set; }

    [Display(Name = "Secure Mail")]
    public string secureMail { get; set; }

    [Display(Name = "Account Type")]
    public string typeOfAccount { get; set; }

    [Display(Name = "Extract Date")]
    public DateTime extractDate { get; set; }

    [Display(Name = "Created Date")]
    public DateTime createdDate { get; set; }
    }
  }

对于这个冗长的问题感到抱歉,因为我不确定我想要准确描述我正在做什么以及如何做的确切问题。我在控制器中注释了很多代码,只是向您展示我尝试过的不同内容。

如果事情不清楚,我可以回答任何问题或更好地描述某些事情。

更新:连接字符串 - 为了保护自己不发布私有数据,我进行了搜索,找到并用db1和db2替换连接名称,并将服务器名称替换为myServer。 我确保在所有事件中都准确输入了名称,并且服务器名称是正确的......所以这两件事不会成为问题。

<connectionStrings>
   <add name="db1Entities" connectionString="metadata=res://*/Models.db1.csdl|res://*/Models.db1.ssdl|res://*/Models.db1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myServer;initial catalog=db1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   <add name="db2Entities" connectionString="metadata=res://*/Models.db2.csdl|res://*/Models.db2.ssdl|res://*/Models.db2.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myServer;initial catalog=db2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

我们为实体类使用元数据类型的部分类......如下所示:

namespace project.Models
{
  public class PartialClasses
  {
  }

  [MetadataType(typeof(object1Metadata))]
  public partial class object1
  { }

  [MetadataType(typeof(object2Metadata))]
  public partial class object2
  { }

}

1 个答案:

答案 0 :(得分:0)

您是否添加或更改了任何架构?如果是这样,最简单的选择是删除两个EDMX并再次创建它们