簡體   English   中英

在C#Windows Forms應用程序中使用MVC項目中的DbContext?

[英]Using the DbContext from an MVC project in a C# Windows Forms Application?

我的解決方案中有兩個項目。 首先是所謂的EXSIS一個MVC項目,第二個是一個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();
    }
}
}

當我運行此程序時,我最初收到一條錯誤消息:

在應用程序配置文件中找不到名為“ exsisDBEntities”的連接字符串。

因此,我進入了EXSIS中的web.config文件,並將以下連接字符串復制到了Backend中的app.config文件中:

<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>

但這給了我一個新的錯誤。 當LastOrder()方法中的以下行運行時:

customers = db.Customers.ToList();

我收到錯誤消息:

類型的未處理的異常“System.Data.Entity.Core.EntityException”發生在EntityFramework.SqlServer.dll

附加信息:基礎提供程序在打開時失敗。

任何有關如何解決此錯誤的幫助將不勝感激。

您缺少另一個項目中的* .edmx文件。 metadata=res://...連接字符串是對edmx文件數據庫的引用。 兩者都是在數據庫優先生成上下文中必需的。

但是,正如@TroyCarlson所指出的,最好將所有這些內容移到兩個項目都可以引用的類庫中。

我有同樣的問題,但是我還不能解決,但是“使用”可能會有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM