Entity Framework - ConnectionString 属性 尚未初始化

Entity Framework - the ConnectionString property has not been initialized

我有以下代码:

using (AtlasEntities dbContext = new AtlasEntities())
{
    List<SqlParameter> parameters = new List<SqlParameter>();
    parameters.Add(new SqlParameter("today", today));

    var results = dbContext
        .MultipleResults($"EXEC [dbo].[GetPolicyPremiums] @today, parameters)
        .AddResult<PremiumDueReminderEntity>()
        .AddResult<PremiumDueReminderEntity>()
        .AddResult<PremiumDueReminderEntity>()
        .AddResult<PremiumDueReminderEntity>()
        .Execute();
/**/
    Mail mail = new Mail()
                    {
                        IsMailSent = false,
                        ErrorFailureCount = 0,
                        Body = "Body",
                        Subject = "Premium Due Reminder"
                    };
    dbContext.Mails.Add(mail);
    dbContext.SaveChanges();
}

AtlasEntities 来自另一个名为 EntityModel 的项目,它作为我的数据层使用 Entity Framework 和 EDMX。

.Execute() 工作得很好,我能够从数据库中获得所需的 results

然而,当我 运行 dbContext.SaveChanges() 代码时,我得到以下堆栈跟踪:

at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at NotificationApp.PremiumNotificationManager.StartCheck() in C:\Projects\LIVE\NotificationApp\Manager\PremiumNotificationManager.cs:line 71

消息是:

The underlying provider failed on Open

内部异常是:

The ConnectionString property has not been initialized

我没有在这个项目的任何地方使用 SqlConnection

两个项目在各自的 App.config 文件中使用相似的连接字符串:

<connectionStrings>
    <add name="AtlasEntities" 
         connectionString="metadata=res://*/Data.AtlasModel.csdl|res://*/Data.AtlasModel.ssdl|res://*/Data.AtlasModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=NAB-SHARMAB\SQLSERVER2019;initial catalog=Live_DB;persist security info=True;user id=AdminTest;password=PasswordTest;multipleactiveresultsets=True;application name=EntityFramework&quot;" 
          providerName="System.Data.EntityClient" />
</connectionStrings>`

我没有发现任何其他人遇到此类错误,也不知道是什么原因造成的。请帮助。

声明并初始化一个空的邮件列表

List<Mail> mails = new List<Mail>();

然后改变

dbContext.Mails.Add(mail);

简单地

mails.Add(mail);

我把保存部分移到另一种方法解决了:

private static void SaveMail(string emailBody)
        {
            using (AtlasEntities dbContext = new AtlasEntities())
            {
                Mail mail = new Mail()
                {
                    IsMailSent = false,
                    ErrorFailureCount = 0,
                    Body = emailBody,
                    Subject = "Premium Due Reminder"
                };
                dbContext.Mails.Add(mail);
                dbContext.SaveChanges();
        }

了解为什么我创建另一个 Context 时没有收到此错误仍然会有所帮助。我不确定为什么它现在有效。