我有ASP.Net MVC
网站。
技术堆栈
当我尝试使用Nuget
命令生成数据库时: -
Enable-Migrations -force
我收到以下异常
'System.Data.Entity.Migrations.DbMigrationsConfiguration`1'的类型初始值设定项引发异常。
以下内容已经交叉检查&我试过: -
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception on a Sub Website
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception
我的App.Config: -
<connectionStrings>
<add name="mydbContext" providerName="MySql.Data.MySqlClient" connectionString="Server=localhost;port=8080;database=mydb;uid=root;password=" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
答案 0 :(得分:2)
此配置对我有用:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<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.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<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 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="MyLocalDatabase" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********" />
</connectionStrings>
</configuration>
由于我的开发计算机中MySql
连接器客户端的安装错误,我收到了与您类似的错误。我在Visual Studio中安装了MySql
个连接器Nuget包,但这还不够。我还需要安装从MySql
网站下载的MySql
客户端。之后,Type
错误就消失了。
这是the download page of MySql connector我手动下载并安装的。{/ p>
注意:如果明确指定提供者的类型,则永远不会使用defaultConnectionFactory
值。在这种情况下,在此设置中配置文件中的值与您无关。见this reference doc page in MySql website中的第3点。
我建议您使用MySql命令行客户端控制台检查计算机中是否正确安装了MySql
。尝试使用连接字符串值从Visual Studio外部连接到服务器。
答案 1 :(得分:2)
出现类型初始化错误是因为invariantName=MySql.Data.MySqlClient
在<providers>
文件的web.config
部分中出现了两次。
删除其中一个(通过注释)解决了这个问题:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<!--<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
<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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
答案 2 :(得分:1)
即使在迁移时遇到此错误。我有一个数据访问层(DAL),其中包含上下文类和引用该数据访问层的Web应用程序。要进行迁移,您必须在DAL的app.congig文件中以及在Web应用程序中的web.config文件中添加连接字符串。从web.config文件中删除默认连接字符串后,它对我有用。 取代这个
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-foo-20190108014329.mdf;Initial Catalog=aspnet-foo-20190138014529;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
作者
<connectionStrings>
<add name="foo" connectionString="Data Source=foo; Initial Catalog=foo; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
这对我有用。
答案 3 :(得分:1)
我找到了解决这个问题的方法。
我的app.config
在configSections:
中缺少部分名称
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
我只是添加了它,就可以了。
答案 4 :(得分:1)
在EF # pylint: skip-file
# flake8: noqa
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from pyspark.sql.types import *
from awsglue.context import GlueContext
from awsglue.job import Job
from pyspark.sql.functions import split
import boto3
from urlparse import urlparse
# Read arguments
args = getResolvedOptions(sys.argv, ['JOB_NAME', 's3target', 's3source'])
# Initialise boto3
client = boto3.client('s3')
# Get all the available files
response = client.list_objects_v2(Bucket = "xxx")
files = [ "s3://xxx/" + obj['Key'] for obj in response['Contents'] if obj['Key'].endswith('.tsv.gz') ]
# Initialise the glue job
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
# Define massive list of fields in the schema
fields = [
StructField("accept_language", StringType(), True),
StructField("browser", LongType(), True),
.... huge list ...
StructField("yearly_visitor", ShortType(), True),
StructField("zip", StringType(), True)
]
schema = StructType(fields)
# Read in data using Spark DataFrame and not an AWS Glue DynamicFrame to avoid issues with non-UTF8 characters
df0 = spark.read.format("com.databricks.spark.csv").option("quote", "\"").option("delimiter", u'\u0009').option("charset", 'utf-8').schema(schema).load(files)
# Remove all rows that are entirely nulls
df1 = df0.dropna(how = 'all')
# Generate a partitioning column
df2 = df1.withColumn('date', df1.date_time.cast('date'))
# Write out in parquet format, partitioned by date, to the S3 location specified in the arguments
ds2 = df2.write.format("parquet").partitionBy("date").mode("append").save(args['s3target'])
# retrieve the tracked files from the initial DataFrame
# you need to access the java RDD instances to get to the partitions information
# The file URIs will be in the following format: u's3://mybucket/mypath/myfile.tsv.gz'
files = []
for p in df0.rdd._jrdd.partitions():
files.extend([f.filePath() for f in p.files().array()])
# Move files to the processed folder
for uri in files:
parsed = urlparse(uri)
client.copy_object(CopySource = {'Bucket': parsed.netloc, 'Key': parsed.path.lstrip('/')}, Bucket = parsed.netloc, Key = 'processed' + parsed.path)
client.delete_object(Bucket = parsed.netloc, Key = parsed.path.lstrip('/'))
job.commit()
中,最有可能是由于Web.config文件格式错误,例如具有两个或多个连接字符串部分,或者连接字符串本身存在问题,例如无效的提供程序。
答案 5 :(得分:1)
删除连接字符串,然后再次添加迁移。连接字符串将自动添加。我遇到了同样的问题,它对我有用。
答案 6 :(得分:1)
只需在配置部分中添加 configSections (如果未添加)。并且 configSections必须是配置部分的第一个子代,就像下面的代码一样。
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ConnectionString"
connectionString="data source=20.20.1.1;initial catalog=myDB;user id=sa;password=123456;persist security info=True;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient"/>
</connectionStrings>
....
<!-- other configuration -->
....
</configuration>
答案 7 :(得分:0)
当我遇到这个问题时,我意识到实体框架没有在配置文件中声明。
所以我添加了
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
在连接字符串
之上答案 8 :(得分:0)
通过将 configSections 设为配置文件中 configuration 的第一个子项来进行修复。
<configuration>
... ...
答案 9 :(得分:0)
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=DESKTOP-ABHI;Initial Catalog=IdentityDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
首先检查web.config文件中的连接字符串。
如果您有两个连接字符串,请注释第二个连接字符串。