MVC 4连接字符串无法登录
本文关键字:登录 字符串 连接 MVC | 更新日期: 2023-09-27 17:50:45
我正在使用MVC 4和实体框架5.0制作一个网站。我应该有一个页面,用户可以看到他的数据(姓名,电子邮件,年龄等)。问题是当试图访问该数据库时,程序中断并返回
" An exception of type 'System.Data.EntityException' occurred in System.Data.Entity.dll but was not handled in user code "
在内部异常中给出这个错误:
`{"Cannot open database '"BookMania.Models.PerfilContext'" requested by the login. The login failed.'r'nLogin failed for user 'TAG000002717''teste'."}`
我认为这可能会发生,因为我在Web中定义了我的连接字符串。配置错误。
`add name="PerfilContexto" connectionString="metadata=res://*/ModeloBookmania.csdl|res://*/ModeloBookmania.ssdl|res://*/ModeloBookmania.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)'v11.0;attachdbfilename=C:'Users'teste.TAG000002717'Documents'BD.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />`
作为" perfilcontext "模型,我在其中调用类Dbcontext
public class PerfilContext :DbContext
{
public DbSet<Perfil> Dados { get; set; }
}
任何想法?: o
更新:连接字符串已经解决,非常感谢用户Pawel Maga。
这个异常仍然存在。
尽管如此,现在给出了一个额外的信息:实体类型Perfil不是当前上下文的模型的一部分。
控制器部分给出了错误:
public ActionResult Perfil(int userid=1)
if(Session["UserID"] == null)
return View(); // if there's no session, it runs blank,cause in the view there's a code to redirect to homepage if there's no session.
var ida=Session["UserID"]; //After login this variable keeps the user id
userid = Convert.ToInt32(ida);//the varaible is converted to int
PerfilContext contexto = new PerfilContext();
/* gives error about "Perfil" wich is the model for the view*/
Perfil conteXto = contexto.Dados.Single(perf=>perf.ID == userid);
return View(conteXto);
`
您设置的连接字符串名称不正确。
你的dbcontext类名:PerfilContext
您的连接字符串名称:PerfilContexto
将connectionstring name更改为PerfilContext或显式地将此名称传递给基构造函数:
public class PerfilContext : DbContext
{
public PerfilContext()
:base("PerfilContexto")
{
}
public DbSet<Perfil> Dados { get; set; }
}