EF6 Select 来自 MySQL Table
EF6 Select from MySQL Table
我有一个现有的 MySQL 数据库 table。我正在创建一个 windows 服务,它将从 MySql table c2b2_callbacks
获取数据并将值传递给我的 DTO class。
下面是我的select声明
public async Task<IEnumerable<Callback>> GetCallbacks()
{
using(MySqlConnection conn = new MySqlConnection(connectionString))
{
try
{
await conn.OpenAsync();
//MySqlTransaction sqlTransaction = await conn.BeginTransactionAsync();
using (M2AContext context = new M2AContext())
{
//context.Database.UseTransaction(sqlTransaction);
// Error
IEnumerable<CallBack> transactions = await context.Transactions.SqlQuery("SELECT * FROM oapi_test.c2b2_callbacks WHERE Status = 0 AND MSISDN REGEXP \'^([a-z]*[a-z]\s*){3}([0-9]*[0-9]){3}([a-z]){1}$\';").ToListAsync();
return transactions;
}
}
catch(Exception e)
{
//log
Console.WriteLine($"Error {e.Message}");
return null;
}
}
}
下面是我的CallBack
class
public class CallBack
{
public string TransID { get; set; }
public DateTime TransTime { get; set; }
public string MSISDN { get; set; }
public Int16 Status { get; set; }
}
以下是我的背景
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class M2AContext: DbContext
{
public M2AContext(): base()
{
}
public M2AContext(DbConnection connection, bool ownedconnection): base(connection, ownedconnection)
{
}
public DbSet<Callbacks> Transactions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
我正在尝试获取交易,但它在 SELECT
语句处失败并抛出此错误
{"Object reference not set to an instance of an object."}
我已经查看了 this 文档,但我不知道为什么会出现此错误。
这是堆栈跟踪
at MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
at MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
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.ExecuteSqlQueryAsync(String sql, Boolean asNoTracking, Nullable`1 streaming, Object[] parameters)
at System.Data.Entity.Internal.InternalSqlSetQuery.GetAsyncEnumerator()
at System.Data.Entity.Infrastructure.DbRawSqlQuery`1.System.Data.Entity.Infrastructure.IDbAsyncEnumerable<TElement>.GetAsyncEnumerator()
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ForEachAsync[T](IDbAsyncEnumerable`1 source, Action`1 action, CancellationToken cancellationToken)
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ToListAsync[T](IDbAsyncEnumerable`1 source, CancellationToken cancellationToken)
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ToListAsync[T](IDbAsyncEnumerable`1 source)
at System.Data.Entity.Infrastructure.DbRawSqlQuery`1.ToListAsync()
at m2a.Repositories.Repository.<GetCallbacks>d__2.MoveNext() in C:\Users\JNyingi\source\repos\m2a\Repositories\Repository.cs:line 38
我认为您需要提供一个映射,否则 EF 将不知道如何绑定到 CallBack 对象
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
应该是
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<CallBack>("c2b2_callbacks");
}
我有一个现有的 MySQL 数据库 table。我正在创建一个 windows 服务,它将从 MySql table c2b2_callbacks
获取数据并将值传递给我的 DTO class。
下面是我的select声明
public async Task<IEnumerable<Callback>> GetCallbacks()
{
using(MySqlConnection conn = new MySqlConnection(connectionString))
{
try
{
await conn.OpenAsync();
//MySqlTransaction sqlTransaction = await conn.BeginTransactionAsync();
using (M2AContext context = new M2AContext())
{
//context.Database.UseTransaction(sqlTransaction);
// Error
IEnumerable<CallBack> transactions = await context.Transactions.SqlQuery("SELECT * FROM oapi_test.c2b2_callbacks WHERE Status = 0 AND MSISDN REGEXP \'^([a-z]*[a-z]\s*){3}([0-9]*[0-9]){3}([a-z]){1}$\';").ToListAsync();
return transactions;
}
}
catch(Exception e)
{
//log
Console.WriteLine($"Error {e.Message}");
return null;
}
}
}
下面是我的CallBack
class
public class CallBack
{
public string TransID { get; set; }
public DateTime TransTime { get; set; }
public string MSISDN { get; set; }
public Int16 Status { get; set; }
}
以下是我的背景
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class M2AContext: DbContext
{
public M2AContext(): base()
{
}
public M2AContext(DbConnection connection, bool ownedconnection): base(connection, ownedconnection)
{
}
public DbSet<Callbacks> Transactions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
我正在尝试获取交易,但它在 SELECT
语句处失败并抛出此错误
{"Object reference not set to an instance of an object."}
我已经查看了 this 文档,但我不知道为什么会出现此错误。
这是堆栈跟踪
at MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
at MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
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.ExecuteSqlQueryAsync(String sql, Boolean asNoTracking, Nullable`1 streaming, Object[] parameters)
at System.Data.Entity.Internal.InternalSqlSetQuery.GetAsyncEnumerator()
at System.Data.Entity.Infrastructure.DbRawSqlQuery`1.System.Data.Entity.Infrastructure.IDbAsyncEnumerable<TElement>.GetAsyncEnumerator()
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ForEachAsync[T](IDbAsyncEnumerable`1 source, Action`1 action, CancellationToken cancellationToken)
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ToListAsync[T](IDbAsyncEnumerable`1 source, CancellationToken cancellationToken)
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ToListAsync[T](IDbAsyncEnumerable`1 source)
at System.Data.Entity.Infrastructure.DbRawSqlQuery`1.ToListAsync()
at m2a.Repositories.Repository.<GetCallbacks>d__2.MoveNext() in C:\Users\JNyingi\source\repos\m2a\Repositories\Repository.cs:line 38
我认为您需要提供一个映射,否则 EF 将不知道如何绑定到 CallBack 对象
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
应该是
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<CallBack>("c2b2_callbacks");
}