ورود

View Full Version : مبتدی: مشکل در اجرای پروژه با Entity Framework



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

ممنون

RIG000
جمعه 06 مرداد 1396, 22:06 عصر
باید داخل view Detail بری و در InnerExcption انقدر پیش بری که به Message برسی تا ببنی دقیقا چه خطایی داره . اما اینجا احتمالا خطای شما از نبود ConnestionString در WebConfig میتونه باشه .

milad_sabz
جمعه 06 مرداد 1396, 23:16 عصر
ممنون آره فکر کنم بخاطر کانکشن استرینگه
این محتویات weconfig :

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>



<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrame workSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform .CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform , Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform .VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform , Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectio nFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />


</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>


<!--<connectionStrings>
<add name="DESKTOP-VN10AA4"
connectionString="Data Source=localhost;
Initial Catalog=Northwind;
User Id=EntityFrameWork_Persianit_Learn;
Password=" />
</connectionStrings>-->
</configuration>


لطفا بگین کانکشن استرینگ چجوری توش تعریف کنم ؟

hakim22
شنبه 07 مرداد 1396, 07:36 صبح
مشکل شما عدم اتصال به دیتابیس SQL است. آدرس SQL ، نام کاربری و رمز عبور رو چک کنید. همینطور مطمئن شوید که سرویس مربوط به SQL در ویندوز در حال اجراست.

RIG000
شنبه 07 مرداد 1396, 11:36 صبح
اینجا رو ببین (https://www.connectionstrings.com/sql-server/)

milad_sabz
شنبه 07 مرداد 1396, 18:38 عصر
ممنون . از کچا چک کنم نام کاربری و رمز رو ؟ از کانکشن استرینگ منظورتونه ؟
سرویس اس کیو ال در حال اجراست اونو مطمعنم

milad_sabz
شنبه 07 مرداد 1396, 19:06 عصر
این محتویات فایل web.config من می باشد . اصولا باید بعد تگ </configSections> کانکشن استرینگ تعریف بشه .






<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>



<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrame workSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>




<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform .CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform , Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform .VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform , Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectio nFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />


</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>



</configuration>


اما من دیتابیس ندارم که توی کانکشن استرینگ بهش معرفی اش کنم من Models دارم که با entityframe work ساختمش (کلاس های dbcontextوperson)
الان باید چجوری کانکشن استرینگمو بنویسم ؟؟؟

ممنون

RIG000
شنبه 07 مرداد 1396, 22:18 عصر
<connectionStrings> <add name="DataBaseContext" connectionString="Data Source=.;Initial Catalog=DataBaseContext;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />



</connectionStrings>