ASP.NET - error al operar con Entity Framework

 
Vista:
sin imagen de perfil

error al operar con Entity Framework

Publicado por Andres (13 intervenciones) el 30/05/2017 18:29:39
tengo una aplicación web MVC que usa entity framework para la persistencia de datos. Creo la base de datos y luego use el asistente para que me cree el controlador CRUD automáticamente "Controlador de MC5 con vistas que usa Entity Framework". Ejecuto el proyecto y me tira una excepción en la línea
1
return View(db.Student.ToList());
y me dice
1
2
3
4
5
6
7
Se produjo una excepción de tipo 'System.Data.Entity.Core.EntityException' en EntityFramework.SqlServer.dll pero no se controló en el código del usuario Y luego el navegador me dice
 
Error de servidor en la aplicación '/'.
Error de inicio de sesión del usuario 'sa'.
Descripción: Excepción no controlada al ejecutar la solicitud Web actual. Revise el seguimiento de la pila para obtener más información acerca del error y dónde se originó en el código.
 
Detalles de la excepción: System.Data.SqlClient.SqlException: Error de inicio de sesión del usuario 'sa

controlador:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 crud;
 
namespace crud.Controllers
{
    public class StudentsController : Controller
    {
        private LearningKOEntities db = new LearningKOEntities();
 
        // GET: Students
        public ActionResult Index()
        {
            return View(db.Student.ToList());
        }
 
        // GET: Students/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Student.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }
 
        // GET: Students/Create
        public ActionResult Create()
        {
            return View();
        }
 
        // POST: Students/Create
        // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
        // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "StudentId,FirstName,LastName,Age,Gender,Batch,Address,Class,School,Domicile")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Student.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
 
            return View(student);
        }
 
        // GET: Students/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Student.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }
 
        // POST: Students/Edit/5
        // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
        // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "StudentId,FirstName,LastName,Age,Gender,Batch,Address,Class,School,Domicile")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }
 
        // GET: Students/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Student.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }
 
        // POST: Students/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            Student student = db.Student.Find(id);
            db.Student.Remove(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Wilfredo Patricio Castillo
Val: 169
Oro
Ha mantenido su posición en ASP.NET (en relación al último mes)
Gráfica de ASP.NET

error al operar con Entity Framework

Publicado por Wilfredo Patricio Castillo (75 intervenciones) el 01/06/2017 01:56:13
Pues como el mensaje te está diciendo, hay un error de inicio de sesión, o está mal la contraseña o hay un dato que no estás considerando, asegúrate que puedes acceder desde el management studio, caso contrario revisa que el servicio esté levantado.

Si todo anda bien desde el management studio, revisa tu cadena de conexión.

Saludos cordiales,
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

error al operar con Entity Framework

Publicado por Andres (13 intervenciones) el 01/06/2017 06:05:31
anda bien el management studio, y el servicio está levantado. como chequeo la cadena de conexión?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar