Nic*_*dum 3 .net c# mysql entity-framework visual-studio-2013
我正在尝试学习如何在MySQL中使用Entity Framework 6.由于我已经有了一个现有的数据库,所以我遵循Model First方法.但是,我总是面对这个有用的错误消息.
在Program.cs中的这行代码中触发错误:
List<Student> students = dbContext.Students.ToList();
Run Code Online (Sandbox Code Playgroud)
错误消息只是说:
System.Configuration.dll中发生未处理的类型'System.Configuration.ConfigurationErrorsException'的异常附加信息:无法识别的属性'name'.
我的第一个假设是我的App.config文件有问题.但是,我不知道name它正在讨论的App.config文件中的哪个属性.我试着寻找现有的答案或可能的解决方案无济于事所以我决定发布我的问题希望你们帮助我.
我在用什么:
我做了什么步骤:
DSWS_Model使用我现有数据库命名的ADO.Net实体数据模型Program.cs中
namespace MySQLEntityFramework6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start?");
Console.ReadLine();
try
{
using (dswsEntities dbContext = new dswsEntities())
{
List<Student> students = dbContext.Students.ToList();
foreach (Student student in students)
{
Console.WriteLine(student.name);
}
}
}
catch (Exception ex)
{
ExceptionUtility.LogException(ex);
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
DSWS_Model.Context.cs
namespace MySQLEntityFramework6
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class dswsEntities : DbContext
{
public dswsEntities()
: base("name=dswsEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Administrator> Administrators { get; set; }
public virtual DbSet<BuildingSetting> BuildingSettings { get; set; }
public virtual DbSet<Evaluation> Evaluations { get; set; }
public virtual DbSet<LockerReservation> LockerReservations { get; set; }
public virtual DbSet<Locker> Lockers { get; set; }
public virtual DbSet<LoginQueue> LoginQueues { get; set; }
public virtual DbSet<Student> Students { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
App.config中
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="dswsEntities" connectionString="metadata=res://*/DSWS_Model.csdl|res://*/DSWS_Model.ssdl|res://*/DSWS_Model.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=root;persistsecurityinfo=True;database=dsws"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
错误日志
Exception Type: System.Configuration.ConfigurationErrorsException
Exception: Unrecognized attribute 'name'. (C:\Users\Nico\Documents\Visual Studio 2013\Projects\MVA\Entity Framework\MySQLEntityFramework6\MySQLEntityFramework6\bin\Debug\MySQLEntityFramework6.vshost.exe.Config line 23)
Source: System.Configuration
Stack Trace:
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
at System.Data.Common.DbProviderFactories.Initialize()
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key, Func`3 handleFailedLookup)
at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at MySQLEntityFramework6.Program.Main(String[] args) in c:\Users\Nico\Documents\Visual Studio 2013\Projects\MVA\Entity Framework\MySQLEntityFramework6\MySQLEntityFramework6\Program.cs:line 19
Target Site: System.Object EvaluateOne(System.String[], System.Configuration.SectionInput, Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object)
Run Code Online (Sandbox Code Playgroud)
提前感谢您的回复.
从您的堆栈跟踪中可以看出,它正在抱怨配置文件中某个DbProviderFactory元素上的"name"属性.您不需要在remove元素上指定name,只需要不变.尝试删除它,事情应该开始工作!
| 归档时间: |
|
| 查看次数: |
7106 次 |
| 最近记录: |