EF6在设计和运行时之间丢失了上下文

时间:2014-07-24 16:56:46

标签: c# entity-framework

我有一个添加到表的EF6 Database-First应用程序。该模型似乎已经完美生成(T4),智能感知显示了我在设计时所期望的。 但是,在运行时,异常:

  

实体类型UsedLoanNumbers不是当前上下文的模型的一部分。"

出现在首先引用UsedLoanNumbers实体的行上:

using (var context = new NLNEntities())
{
    UsedLoanNumbers uln = context.UsedLoanNumbers.Last();
    uln.UserID = userID;
    uln.AssignedDateTime = DateTime.Now;
    uln = context.UsedLoanNumbers.Add(uln);
    this.LoanNumber = uln.LoanNumber.ToString();
    context.SaveChanges();
}

我在另一个执行日志记录的DLL中有第二个EF上下文...它们的名称完全不同,但都在同一个数据库中修改数据:          

我做错了什么?

这里是上下文代码:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace NewLoanNumber
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class NLNEntities : DbContext
    {
        public NLNEntities() : base("name=NLNEntities") { }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }

            public virtual DbSet<ErrorLogging> ErrorLoggings { get; set; }
            public virtual DbSet<LogEntryPriority> LogEntryPriorities { get; set; }
            public virtual DbSet<LogEntryType> LogEntryTypes { get; set; }
            public virtual DbSet<ProcessLog> ProcessLogs { get; set; }
            public virtual DbSet<UsedLoanNumbers> UsedLoanNumbers { get; set; }
    }
}

连接字符串:

name="NLNEntities" connectionString="metadata=res://*/LoggingModel.csdl|res://*/LoggingModel.ssdl|res://*/LoggingModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CSTestLSDW;initial catalog=LoanServicingDW;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

name="LoggingEntities" connectionString="metadata=res://*/LoggingModel.csdl|res://*/LoggingModel.ssdl|res://*/LoggingModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CSTestLSDW;initial catalog=LoanServicingDW;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

0 个答案:

没有答案