我仔细阅读了有关该论点的类似问题的所有答案,但没有找到适合我问题的解决方案。
<entityFramework>
<defaultConnectionFactory ype="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings configSource="Connections.config" />
<add name="DataContext"
connectionString = "Server=localhost; Port=3306; Database=dbName; uid=<correct user>; pwd=<correct password>" providerName="MySql.Data.MySqlClient"/>
using MyApp.DataAccess;
using System;
using System.Linq;
using System.Security.Principal;
namespace myApp.Core
{
public class UserManager : IUserManager
{
private UserAccessLevels _level;
(...)
private DataContext DataContext { get; }
public UserManager(DataContext dc) { // dc is injected by DI container
DataContext = dc ?? throw new ArgumentNullException(nameof(DataContext));
var winUserId = WindowsIdentity.GetCurrent();
var loginName = winUserId.Name.Substring(winUserId.Name.LastIndexOf('\\') + 1);
// In the following line I have the error:
var usr = DataContext.AppUser.SingleOrDefault(x => x.LoginId == loginName);
我真的很感谢任何提示或真实的照亮...感谢大家
更新:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public partial class DataContext : DbContext
{
(... IDbSets registration ...)
public DataContext() : base("DataContext") {
this.Configuration.ProxyCreationEnabled = false;
}
(... the rest of code ...)
同样,请记住,代码迁移工作正常;这是一个非常奇怪的行为。
答案 0 :(得分:0)
在connections.config文件中,以connectionStrings
作为主Web.config
文件中节点的名称,创建一个节,这里的代码可以解决您的问题。
<connectionStrings>
<add name="DataContext"
connectionString = "Server=localhost; Port=3306; Database=dbName; uid=<correct user>; pwd=<correct password>" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>