【发布时间】:2019-06-08 10:32:05
【问题描述】:
//学生管理员
public class Student
{
[Key]
public int StudentId { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Emial { get; set; }
public string Role { get; set; }
}
//StudentConroller(这里我叫存储过程)
public class AccountController : Controller
{
public ApplicationDbClass applicationDbClass;
public AccountController()
{
applicationDbClass = new ApplicationDbClass();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Student student)
{
var v1 = new SqlParameter();
v1.ParameterName = "@role";
v1.SqlDbType = SqlDbType.NVarChar;
v1.Value = "Admin";
var v2 = new SqlParameter();
v2.ParameterName = "@count";
v2.SqlDbType = SqlDbType.Int;
try
{
var result = applicationDbClass.Students.SqlQuery("StudentProcedure @role,@count OUT", v1, v2).ToArray();
}
catch(Exception e)
{
var m = e.Message;
}
return RedirectToAction("Welcome", "Student");
}
}
//存储过程
CREATE OR ALTER PROCEDURE StudentProcedure
@role NVARCHAR(30),
@count INT OUTPUT
AS
BEGIN
SELECT @count=COUNT(dbo.Students.Role)
FROM dbo.Students
WHERE Role=@role;
END
//DbContext 类
public class ApplicationDbClass : DbContext
{
public ApplicationDbClass() : base()
{
Database.SetInitializer<ApplicationDbClass>(new DropCreateDatabaseIfModelChanges<ApplicationDbClass>());
}
public DbSet<Student> Students { get; set; }
public DbSet<LogTable> LogTables { get; set; }
}
// 这里我使用代码优先方法来处理数据库,使用实体框架调用用户创建的存储过程。如果我对存储过程进行一些更改,它将不会直接反映。请给我任何解决方案以反映更改。
【问题讨论】:
-
@count是一个OUTPUT参数,因此您必须相应地标记.Direction。 -
添加
v2.ParameterDirection = ParameterDirection.Output;。我输入的默认方向。 -
我得到这个异常“Procedure or function StudentProcedure has too many arguments specified”。
标签: asp.net sql-server entity-framework model-view-controller