내 솔루션에는 두 가지 프로젝트가 있습니다. 첫 번째는 EXSIS라는 MVC 프로젝트이고 두 번째는 Backend라는 C# Windows Forms 응용 프로그램입니다. EXSIS에는 데이터베이스 파일 exsisDB.mdf가 들어 있으며 데이터베이스의 첫 번째 방법을 사용하여 작성되었습니다. 이제는 매일 특정 시간에 데이터베이스에 레코드를 추가하기 위해 EXSIS의 DbContext (exsisDBEntities)에 액세스하여 백엔드에서 액세스 할 수 있기를 원합니다. EXSIS를 Backend에 대한 참조로 추가했습니다. 여기 C# Windows Forms 응용 프로그램에서 MVC 프로젝트의 DbContext 사용?
은 백엔드에서 Form1의 코드입니다 이그래서 나는 EXSIS에서 web.config 파일에 가서 백엔드에서 app.config 파일을 통해 다음과 같은 연결 문자열을 복사 :'exsisDBEntities'라는 이름의 연결 문자열은 응용 프로그램에서 찾을 수 없습니다 :이 실행 때 나는 원래 말하는 오류가 발생했습니다
설정 파일.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="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\exsisDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
을하지만 그건 그냥 나에게 새로운 오류를 주었다. 하여 LastOrder() 메소드에 다음 줄이 실행되면 : 유형의
처리되지 않은 예외 'System.Data.Entity.Core.EntityException이'에서 발생
customers = db.Customers.ToList();
을 나는 오류 메시지가 EntityFramework.SqlServer.dll
추가 정보 : 기본 공급자가 열지 못했습니다.
이 오류를 해결하는 방법에 대한 도움을 주시면 매우 감사하겠습니다.
1. 당신의 MVC 프로젝트 중과로 (엔티티 프레임 워크) 코드 데이터 액세스를 이동 고려 새로운 "데이터"프로젝트. 2. MVC 프로젝트에서 데이터베이스에 성공적으로 액세스 할 수 있습니까? –
저장소 패턴을 보자. – Jonesopolis
@Jonesy : 우선, 그 주석은 질문과 관련이 없다. 두 번째로, EF는 저장소 패턴을 따른다. –