我试图将我的API(使用EF6)与MySQL集成。 我已经安装了MySql.Data.Entity包(版本6.9.7)。
最初,我使用的是Sql Server,因此我的日期字段映射使用了DateTimeOffSet类型。但是,当使用MySql时,我遇到了这种类型的麻烦,因此我将其更改为DateTime。迁移成功运行,在MySql中创建数据库,但在日志末尾显示错误:
PM> Update-Database -StartUpProjectName WMoney.WebApi -Verbose
Using StartUp project 'WMoney.WebApi'.
Using NuGet project 'WMoney.Persistence.EntityFramework'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'wmoney' (DataSource: localhost, Provider: MySql.Data.MySqlClient, Origin: Configuration).
No pending explicit migrations.
System.ArgumentException: The underlying provider does not support the type 'datetimeoffset'.
at MySql.Data.MySqlClient.MySqlProviderManifest.GetEdmType(TypeUsage storeType)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildColumnModel(EdmProperty property, ModelMetadata modelMetadata, IDictionary`2 annotations)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildAlterColumnOperation(String table, EdmProperty targetProperty, ModelMetadata targetModelMetadata, EdmProperty sourceProperty, ModelMetadata sourceModelMetadata)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<FindAlteredColumns>b__24a(<>f__AnonymousType2c`2 <>h__TransparentIdentifier242)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion)
at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The underlying provider does not support the type 'datetimeoffset'.
除了错误之外,还创建了数据库,我可以运行我的Api。但是,当尝试对DB进行查询时,会发生同样的错误:&#34;基础提供程序不支持类型&#39; datetimeoffset&#39;。&#34;
我已经从我的代码中更改了对DateTimeOffSet的所有引用,但错误仍然存在。
有谁知道我该怎么做才能解决这个问题?
由于
更新: 根据DevilSuichiro的要求,我添加了更多信息:
MY CONTEXT CLASS:
namespace WMoney.Persistence.EntityFramework
{
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class WMoneyContext : DbContext, IWMoneyContext
{
public WMoneyContext()
: base("WMoneyConnectionString")
{
}
public IUserRepository UserRepository
{
get { return new UserRepository(this); }
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Transaction> Transactions { get; set; }
public DbSet<TransactionType> TransactionTypes { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Account>()
.HasKey(a => a.AccountId)
.Map(a => a.ToTable("TbAccount", "dbo"));
modelBuilder.Entity<Account>()
.HasRequired(a => a.User)
.WithMany(a => a.Accounts)
.HasForeignKey(a => a.UserId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Category>()
.HasKey(a => a.CategoryId)
.Map(a => a.ToTable("TbCategory", "dbo"));
modelBuilder.Entity<Category>()
.HasRequired(a => a.User)
.WithMany(a => a.Categories)
.HasForeignKey(a => a.UserId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Transaction>()
.HasKey(a => a.TransactionId)
.Map(a => a.ToTable("TbTransaction", "dbo"));
modelBuilder.Entity<Transaction>()
.HasRequired(a => a.Account)
.WithMany(a => a.Transactions)
.HasForeignKey(a => a.AccountId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Transaction>()
.HasRequired(a => a.Category)
.WithMany(a => a.Transactions)
.HasForeignKey(a => a.CategoryId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Transaction>()
.HasRequired(a => a.TransactionType)
.WithMany(a => a.Transactions)
.HasForeignKey(a => a.TransactionTypeId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<TransactionType>()
.HasKey(a => a.TransactionTypeId)
.Map(a => a.ToTable("TbTransactionType", "dbo"));
modelBuilder.Entity<User>()
.HasKey(a => a.UserId)
.Map(a => a.ToTable("TbUser", "dbo"));
}
}
}
MY TRANSACTION CLASS(仅限日期字段)
namespace WMoney.Persistence.Model
{
public class Transaction
{
public int TransactionId { get; set; }
public int TransactionTypeId { get; set; }
public DateTime Created { get; set; }
public DateTime Date { get; set; }
public int CategoryId { get; set; }
public int AccountId { get; set; }
public decimal Value { get; set; }
[StringLength(200)]
public string Description { get; set; }
public virtual TransactionType TransactionType { get; set; }
public virtual Category Category { get; set; }
public virtual Account Account { get; set; }
}
}
MY CLASS呼叫数据库 错误发生在行中: var existentUser = await _userRepository.AsQueryable()。GetByEmail(email);
namespace WMoney.Core
{
public class UserCore : IUserCore
{
public IUserRepository _userRepository;
public UserCore(IWMoneyContext wMoneyContext)
{
_userRepository = wMoneyContext.UserRepository;
}
public async Task<User> CreateUserAsync(string email, string password)
{
var existentUser = await _userRepository.AsQueryable().GetByEmail(email);
if (existentUser != null)
{
throw new DuplicateWaitObjectException();
}
var user = new User
{
Email = email,
Password = password
};
await _userRepository.AddAsync(user, true);
return user;
}
public async Task<bool> CheckUserAsync(string email, string password)
{
var user = await _userRepository.AsQueryable().GetByEmail(email);
if (user != null)
{
if (user.Password == password)
{
return true;
}
else
{
return false;
}
}
else
{
throw new ArgumentException(string.Format("There's no user with email '{0}'", email));
}
}
}
}