【发布时间】:2020-09-19 19:21:37
【问题描述】:
我有以下代码:
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 的项目,该项目使用带有 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
消息是:
底层提供程序在打开时失败
内部异常是:
ConnectionString 属性尚未初始化
我没有在这个项目的任何地方使用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="data source=NAB-SHARMAB\SQLSERVER2019;initial catalog=Live_DB;persist security info=True;user id=AdminTest;password=PasswordTest;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>`
我没有发现其他任何人遇到此类错误,也不知道是什么原因造成的。请帮忙。
【问题讨论】:
-
你能显示调用存储过程和添加新邮件之间的代码吗?在您的示例中应该代替
/**/的代码。
标签: c# entity-framework-6 connection-string