2014-07-07 30 views
0

我的解決方案中有兩個項目。第一個是名爲EXSIS的MVC項目,第二個是名爲Backend的C#Windows窗體應用程序。 EXSIS包含數據庫文件exsisDB.mdf,並使用數據庫優先方法構建。現在我想要做的就是在後端訪問EXSIS的DbContext(稱爲exsisDBEntities),以便每天在特定時間向我的數據庫添加記錄。我添加了EXSIS作爲後端的參考。在C#Windows窗體應用程序中使用MVC項目中的DbContext?

這裏是在後端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文件並在後臺複製對面的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

其他信息:底層提供程序在打開時失敗。

任何幫助如何解決這個錯誤將不勝感激。

+3

1.考慮移動數據接入(實體框架)碼出你的MVC項目和進入一個新的「數據」項目。 2.你能否成功地從你的MVC項目訪問數據庫? –

+0

查看存儲庫模式 – Jonesopolis

+0

@Jonesy:首先,該評論與問題無關;其次,EF遵循存儲庫模式。 –

回答

0

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

但是,正如@TroyCarlson指出的那樣,最好將所有這一切都轉移到兩個項目都可以引用的類庫中。

0

我有同樣的問題,但是卻「利用」我解決不了的還可能是有用的:

using(exsisDBEntities db = new exsisDBEntities()) 
{ 
    //your code 
} 
相關問題