如何使用 ASP.NET MVC 项目参考从控制台应用程序使用 MYSQL DbContext of ASP.NET MVC
How to use MYSQL DbContext of ASP.NET MVC from console application using ASP.NET MVC project reference
我正在尝试从 .NET 控制台应用程序编写 Windows 服务,该应用程序执行将数据发布到 MySQL 数据库中的操作。我正在尝试使用 ASP.NET 项目中的 DbContext
class,但是当我 运行 控制台应用程序时,数据不会发布。我正在使用 Topshelf 依赖项来创建 Windows 服务并使调试更容易。
我的控制台应用程序代码如下所示:
public class UpdateReceivePost
{
private readonly Timer timer;// To establish time for service
public UpdateReceivePost()
{
timer = new Timer(1000) { AutoReset = true }; // the service will start in one minute
timer.Elapsed += TimerElapsed;//calls a timeelapsed mehodd
}
protected IDbFactory DbFactory { get; private set; }
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
UserDbContext dbContext = new UserDbContext();
ReceivePost receivePost = new ReceivePost()
{
Status = "pending"
};
dbContext.receivePosts.Add(receivePost);
dbContext.SaveChanges();
// string[] lines = new string[] { DateTime.Now.ToString() };
// File.AppendAllLines(@"F:\nepal.txt", lines);
}
public void Start()
{
timer.Start();//start the timer
}
public void Stop()
{
timer.Stop(); //stops the timer
}
}
您应该将 dbContext class 包装在一个 using 语句中,以便在您完成发布到数据库后将其处理掉。
The lifetime of the context begins when the instance is created and
ends when the instance is either disposed or garbage-collected. Use
using if you want all the resources that the context controls to be
disposed at the end of the block. When you use using, the compiler
automatically creates a try/finally block and calls dispose in the
finally block.
Install-Package MySQL.Data -Version 6.9.9
Install-Package MySql.Data.Entity -Version 6.9.10
尝试重新安装这些版本的软件包,看看错误是否消失。
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
ReceivePost receivePost = new ReceivePost()
{
Status = "pending"
};
using(UserDbContext dbContext = new UserDbContext())
{
dbContext.receivePosts.Add(receivePost);
dbContext.SaveChanges();
}
// string[] lines = new string[] { DateTime.Now.ToString() };
// File.AppendAllLines(@"F:\nepal.txt", lines);
}
我正在尝试从 .NET 控制台应用程序编写 Windows 服务,该应用程序执行将数据发布到 MySQL 数据库中的操作。我正在尝试使用 ASP.NET 项目中的 DbContext
class,但是当我 运行 控制台应用程序时,数据不会发布。我正在使用 Topshelf 依赖项来创建 Windows 服务并使调试更容易。
我的控制台应用程序代码如下所示:
public class UpdateReceivePost
{
private readonly Timer timer;// To establish time for service
public UpdateReceivePost()
{
timer = new Timer(1000) { AutoReset = true }; // the service will start in one minute
timer.Elapsed += TimerElapsed;//calls a timeelapsed mehodd
}
protected IDbFactory DbFactory { get; private set; }
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
UserDbContext dbContext = new UserDbContext();
ReceivePost receivePost = new ReceivePost()
{
Status = "pending"
};
dbContext.receivePosts.Add(receivePost);
dbContext.SaveChanges();
// string[] lines = new string[] { DateTime.Now.ToString() };
// File.AppendAllLines(@"F:\nepal.txt", lines);
}
public void Start()
{
timer.Start();//start the timer
}
public void Stop()
{
timer.Stop(); //stops the timer
}
}
您应该将 dbContext class 包装在一个 using 语句中,以便在您完成发布到数据库后将其处理掉。
The lifetime of the context begins when the instance is created and ends when the instance is either disposed or garbage-collected. Use using if you want all the resources that the context controls to be disposed at the end of the block. When you use using, the compiler automatically creates a try/finally block and calls dispose in the finally block.
Install-Package MySQL.Data -Version 6.9.9
Install-Package MySql.Data.Entity -Version 6.9.10
尝试重新安装这些版本的软件包,看看错误是否消失。
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
ReceivePost receivePost = new ReceivePost()
{
Status = "pending"
};
using(UserDbContext dbContext = new UserDbContext())
{
dbContext.receivePosts.Add(receivePost);
dbContext.SaveChanges();
}
// string[] lines = new string[] { DateTime.Now.ToString() };
// File.AppendAllLines(@"F:\nepal.txt", lines);
}