milad_sabz
جمعه 06 مرداد 1396, 18:57 عصر
با سلام دوستان عزیز .
من تازه mvc شروع کردم و پکیج انتیتی فریم ورک را اینستال کردم رو یک پروژه ساده که این 2 تا مدل را دارد ولی موقع ران این ارور میده :
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL
موقعی که پروژه رو ران می کنم این ارور رو می ده :http://s9.picofile.com/file/8301887768/New_Text_Document_2_.bmp
person,DatabaseContext
کد دو کلاس بالا :
کلاس دیتابس کانتکس :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EntityFrameWork_Persianit_Learn.Models
{
public class DatabaseContext:System.Data.Entity.DbContext
{
public DatabaseContext()
{
}
static DatabaseContext()
{
System.Data.Entity.Database.SetInitializer(
new System.Data.Entity.DropCreateDatabaseIfModelChange s<DatabaseContext>());
}
public System.Data.Entity.DbSet<Person> People { get; set; }
}
}
کد کلاس پرسون :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EntityFrameWork_Persianit_Learn.Models
{
public class Person
{
public Person()
{
}
public Person(int id,string fullname)
{
this.ID = id;
this.FullName = fullname;
}
public int ID { get; set; }
public string FullName { get; set; }
public override string ToString()
{
return string.Format("{0}{1}",this.ID,this.FullName);
}
}
}
و یک هوم کنترلر نیز ساختم از همون databasecontext اینهریت شده بود که متد های :
create,edit,delete,details و ... را داشت اینم کد هوم کنترلر :}
And HomeControll :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EntityFrameWork_Persianit_Learn.Models;
namespace EntityFrameWork_Persianit_Learn.Controllers {
public class HomeController : Controller
{
private DatabaseContext db = new DatabaseContext();
// GET: Home
public ActionResult Index()
{
return View(db.People.ToList());
}
// GET: Home/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: Home/Create
public ActionResult Create()
{
return View();
}
// POST: Home/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,FullName")] Person person)
{
if (ModelState.IsValid)
{
db.People.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
// GET: Home/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// POST: Home/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,FullName")] Person person)
{
if (ModelState.IsValid)
{
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
// GET: Home/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// POST: Home/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Person person = db.People.Find(id);
db.People.Remove(person);
db.SaveChanges();
return RedirectToAction("Index");
}
//class Dispode yek interface hast
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
این هم تصویر ارور :
:http://s9.picofile.com/file/8301887768/New_Text_Document_2_.bmp
ممنون
من تازه mvc شروع کردم و پکیج انتیتی فریم ورک را اینستال کردم رو یک پروژه ساده که این 2 تا مدل را دارد ولی موقع ران این ارور میده :
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL
موقعی که پروژه رو ران می کنم این ارور رو می ده :http://s9.picofile.com/file/8301887768/New_Text_Document_2_.bmp
person,DatabaseContext
کد دو کلاس بالا :
کلاس دیتابس کانتکس :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EntityFrameWork_Persianit_Learn.Models
{
public class DatabaseContext:System.Data.Entity.DbContext
{
public DatabaseContext()
{
}
static DatabaseContext()
{
System.Data.Entity.Database.SetInitializer(
new System.Data.Entity.DropCreateDatabaseIfModelChange s<DatabaseContext>());
}
public System.Data.Entity.DbSet<Person> People { get; set; }
}
}
کد کلاس پرسون :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EntityFrameWork_Persianit_Learn.Models
{
public class Person
{
public Person()
{
}
public Person(int id,string fullname)
{
this.ID = id;
this.FullName = fullname;
}
public int ID { get; set; }
public string FullName { get; set; }
public override string ToString()
{
return string.Format("{0}{1}",this.ID,this.FullName);
}
}
}
و یک هوم کنترلر نیز ساختم از همون databasecontext اینهریت شده بود که متد های :
create,edit,delete,details و ... را داشت اینم کد هوم کنترلر :}
And HomeControll :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EntityFrameWork_Persianit_Learn.Models;
namespace EntityFrameWork_Persianit_Learn.Controllers {
public class HomeController : Controller
{
private DatabaseContext db = new DatabaseContext();
// GET: Home
public ActionResult Index()
{
return View(db.People.ToList());
}
// GET: Home/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: Home/Create
public ActionResult Create()
{
return View();
}
// POST: Home/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,FullName")] Person person)
{
if (ModelState.IsValid)
{
db.People.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
// GET: Home/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// POST: Home/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,FullName")] Person person)
{
if (ModelState.IsValid)
{
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
// GET: Home/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// POST: Home/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Person person = db.People.Find(id);
db.People.Remove(person);
db.SaveChanges();
return RedirectToAction("Index");
}
//class Dispode yek interface hast
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
این هم تصویر ارور :
:http://s9.picofile.com/file/8301887768/New_Text_Document_2_.bmp
ممنون