Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows窗体应用程序中使用MVC项目中的DbContext?_C#_Asp.net Mvc_Winforms_Dbcontext - Fatal编程技术网

C# 在Windows窗体应用程序中使用MVC项目中的DbContext?

C# 在Windows窗体应用程序中使用MVC项目中的DbContext?,c#,asp.net-mvc,winforms,dbcontext,C#,Asp.net Mvc,Winforms,Dbcontext,我的解决方案中有两个项目。第一个是名为EXSIS的MVC项目,第二个是名为Backend的C Windows窗体应用程序。EXSIS包含数据库文件exsisDB.mdf,并使用数据库优先方法构建。现在,我希望能够在后端访问EXSIS的DbContext,称为exsisDBEntities,以便在每天的特定时间向数据库添加记录。我添加了EXSIS作为后端的参考 以下是后端中Form1的代码: using System; using System.Collections.Generic; using

我的解决方案中有两个项目。第一个是名为EXSIS的MVC项目,第二个是名为Backend的C Windows窗体应用程序。EXSIS包含数据库文件exsisDB.mdf,并使用数据库优先方法构建。现在,我希望能够在后端访问EXSIS的DbContext,称为exsisDBEntities,以便在每天的特定时间向数据库添加记录。我添加了EXSIS作为后端的参考

以下是后端中Form1的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EXSIS.Models;

namespace Backend
{
    public partial class Form1 : Form
    {
    exsisDBEntities db = new exsisDBEntities();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load_1(object sender, EventArgs e)
    {
        System.Threading.TimerCallback callback = new System.Threading.TimerCallback(ProcessTimerEvent);

        var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 1, 0, 0);

        if (DateTime.Now < dt)
        {
            var timer = new System.Threading.Timer(callback, null, dt - DateTime.Now, TimeSpan.FromHours(24));
        }
    }

    private void ProcessTimerEvent(object obj)
    {
        LastOrder();
    }

    private void LastOrder()
    {
        List<Customer> customers = new List<Customer>();
        customers = db.Customers.ToList();
        foreach (Customer customer in db.Customers)
        {
            DateTime LastOrderDate = Convert.ToDateTime(customer.Transactions.Last().Date);
            TimeSpan TimeSinceLastOrder = DateTime.Now - LastOrderDate;
            if (TimeSinceLastOrder.TotalDays > 30)
            {
                Notification n = new Notification();
                n.NotificationID = db.Notifications.Last().NotificationID + 1;
                n.DateGenerated = DateTime.Now;
                n.NotificationType = "Last Order";
                n.CustID = customer.CustID;

                NotificationLink nl = new NotificationLink();
                nl.NotificationLinkID = db.NotificationLinks.Last().NotificationLinkID + 1;
                nl.NotificationID = n.NotificationID;
                nl.RepID = customer.RepID;

                db.Notifications.Add(n);
                db.NotificationLinks.Add(nl);
            }
        }
        db.SaveChanges();
    }
}
}
<connectionStrings>
<add name="exsisDBEntities" connectionString="metadata=res://*/Models.EXSISModel.csdl|res://*/Models.EXSISModel.ssdl|res://*/Models.EXSISModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\exsisDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
我收到错误消息:

EntityFramework.SqlServer.dll中发生类型为“System.Data.Entity.Core.EntityException”的未处理异常

其他信息:基础提供程序在打开时失败


非常感谢您提供有关如何解决此错误的任何帮助。

您丢失了另一个项目中的*.edmx文件。元数据=res://... 连接字符串是对edmx文件和数据库的引用。在数据库第一次生成上下文中,这两者都是必需的


然而,正如@TroyCarlson所指出的,最好将所有这些都转移到两个项目都可以引用的类库中。

我有同样的问题,但我还不能解决它,但使用以下方法可能会有用:

using(exsisDBEntities db = new exsisDBEntities())
{
     //your code
}

1.考虑将数据访问实体框架代码移出MVC项目并进入新的数据项目。2.您能否从MVC项目成功访问数据库?请查看存储库pattern@Jonesy:首先,该评论与问题无关;其次,EF遵循存储库模式。
using(exsisDBEntities db = new exsisDBEntities())
{
     //your code
}