C#Entity Framework:不支持关键字:’port’

您好我有一个以上的项目连接到CodeFirst Entity Framework的某个数据库。

除了一个顽固的项目外,所有项目都能成功连接。

我得到的错误是: Keyword not supported: 'port'

我查看了无数的stackoverflow问题,mysql论坛,entity framework论坛等,包括:

MappingException Edm.String与SqlServer.varbinary不兼容

MySQL的连接字符串中不支持关键字

不支持关键字:’元数据’+ MySQL

我的连接字符串如下所示: server=myservername;port=3306;uid=myaccount;database=mydb;pwd=mypwd123

我的db.cs文件如下所示:

 public partial class MyDB : DbContext { public MyDB () : base("server=myservername;port=3306;uid=myaccount;database=mydb;pwd=mypwd123") { Logger.Trace("test123"); } public virtual DbSet MyItems { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity() .Property(e => e.Content) .IsUnicode(false); } } 

当我从连接字符串中删除port:3306 ,我得到: System.Data.Entity.Core.MappingException: Schema specified is not valid. Errors: (8,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.DateTime[Nullable=False,DefaultValue=,Precision=]' of member 'Time' in type 'something.Model.MyItem' is not compatible with 'SqlServer.timestamp[Nullable=False,DefaultValue=,MaxLength=8,FixedLength=True,StoreGeneratedPattern=Identity]' of member 'time' in type 'CodeFirstDatabaseSchema.MyItem'. at System.Data.Entity.Core.Mapping.StorageMappingItemCollection.Init(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders, IList`1 filePaths, Boolean throwOnError) at System.Data.Entity.Core.Mapping.StorageMappingItemCollection..ctor(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders) at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.ToStorageMappingItemCollection(DbDatabaseMapping databaseMapping, EdmItemCollection itemCollection, StoreItemCollection storeItemCollection) at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping) at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping) at System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 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.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) at System.Data.Entity.DbSet`1.Add(TEntity entity) at MyFunction(Int32 userId, String id, String type, String contentJsonString) in System.Data.Entity.Core.MappingException: Schema specified is not valid. Errors: (8,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.DateTime[Nullable=False,DefaultValue=,Precision=]' of member 'Time' in type 'something.Model.MyItem' is not compatible with 'SqlServer.timestamp[Nullable=False,DefaultValue=,MaxLength=8,FixedLength=True,StoreGeneratedPattern=Identity]' of member 'time' in type 'CodeFirstDatabaseSchema.MyItem'. at System.Data.Entity.Core.Mapping.StorageMappingItemCollection.Init(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders, IList`1 filePaths, Boolean throwOnError) at System.Data.Entity.Core.Mapping.StorageMappingItemCollection..ctor(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders) at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.ToStorageMappingItemCollection(DbDatabaseMapping databaseMapping, EdmItemCollection itemCollection, StoreItemCollection storeItemCollection) at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping) at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping) at System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 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.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) at System.Data.Entity.DbSet`1.Add(TEntity entity) at MyFunction(Int32 userId, String id, String type, String contentJsonString) in

我正在使用MySql Connector而不是Sql Server …

我和我的团队的其他成员完全不同意。

编辑:这是我的Web.Config

使用的基本DbContext 构造函数的参数称为nameOrConnectionString 。 因此,它支持配置文件中的连接字符串的名称,或者在您的情况下支持实际的连接字符串。

后者的问题是它不允许指定提供程序名称,因为前者来自配置,在这种情况下,EF使用defaultConnectionFactory配置元素中指定的那个,在您的情况下是System.Data.Entity.Infrastructure.SqlConnectionFactory ,换句话说–Sql Server ,因此port不支持exception。

有几种方法可以解决这个问题。

(A)更改defaultConnectionFactory配置:

  

(B)使用命名配置连接字符串并明确指定提供者:

    

并将构造函数更改为

 public MyDB() { // ... } 

或者如果名称与您的DbContext派生类名称不同:

 public MyDB() : base(connection_string_name) { // ... } 

(C)使用DbConfigurationTypeAttribute

 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class MyDB : DbContext { // ... } 

我在Core 2上开发Web应用程序时遇到了这个问题。我必须在配置应用程序的Startup.cs文件中将SqlServer使用的默认数据库连接更改为MySql。

在此处输入图像描述

使用ASP.net核心和数据库时出现类似于上面列出的错误。 带有ASP.net核心的默认数据库提供程序是SQL Server,但是,如果您使用的是其他提供程序,例如PostgreSQL,并且未在startup.cs中正确编写或配置DBContext代码

例如 – 下面的代码是为了连接到PostgresSQL而编写的,那么它将导致错误ArgumentException:不支持关键字:’port’。

public void ConfigureServices(IServiceCollection services){services.AddMvc(); var connection = @“Server = localhost; Port = 5432; Database = NewDB; User Id = xxxxx; Password = cr @ aaaa;”; services.AddDbContext(options => options.UseSqlServer(connection));

原因是用户试图连接到PostgreSQL但在配置PostgreSQL字符串后更改了默认的Option UseSQLServer。

要解决此问题,请更改选项


options.UseSqlServer(connection)) – > options.UseNpgsql(connection))