Tomek Krupa Tomek Krupa - 1 year ago 138
C# Question

Entity Framework Code First, MVC, SqlException

I have a problem with code-first approach

My model:

public class Articles
public int ArticleID { get; set; }

[Required(ErrorMessage ="Wymagane podanie tytułu!")]
public string Title { get; set; }
[Required(ErrorMessage = "Wymagane podanie krótkiego opisu!")]
public string ShortDesc { get; set; }
[Required(ErrorMessage = "Wymagane podanie treści!")]
public string Content { get; set; }
public string Author { get; set; }
public string Tags { get; set; }

My connection strings in

<add name="ArticlesSConnectionString"
connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=BazaA;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />


public class ArticlesContext : DbContext
public ArticlesContext() : base("ArticlesConnectionString") { }
public DbSet<Articles> ArticlesS { get; set; }


public class AdminController : Controller
// GET: Admin
public ActionResult Index()
return View();

public ActionResult addArticle(Articles article)
if (!ModelState.IsValid) { return View("Index", article); }
ArticlesContext db = new ArticlesContext();


return View("Index");


An exception of type 'System.Data.SqlClient.SqlException' occurred in
EntityFramework.dll but was not handled in user code

on line:


I have no idea what is wrong...

Answer Source

Got to services and start SQL Server or your Conn string is not good I think that you must have Context class name inside Connection string,

<add name="ArticlesContext" 
     connectionString="Data Source=(LocalDb)\v11.0;AttachDbFileName=|DataDirectory|\BazaA.mdf; Initial Catalog=BazaA;Integrated Security=SSPI" 
     providerName="System.Data.SqlClient" />  

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download