无法使用实体框架从ASP.NET MVC连接到SQL Server [英] Cannot connect to SQL server from ASP.NET MVC with entity framework

查看:109
本文介绍了无法使用实体框架从ASP.NET MVC连接到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC应用程序的新手。我的问题是在我尝试连接到Sql Server2008时。这给了我错误: -

I'm New to MVC Application . My problem is while I'm trying to connect to Sql Server2008 . This is giving me the error :-

Cannot open database "rsbenggmvc.Models.RsbEnggContext" requested by the login. The login failed.
Login failed for user 'DEEPAK-PC\DEEPAK'.





项目名称: - rsbenggmvc

上下文类: - RsbEnggContext

服务器名称: - DEEPAK-PC \ DEEPAK

无密码。



我是什么尝试过:



在我的HomeController索引中的行动是: -



公共ActionResult索引(int id)

{

RsbEnggContext rsbcontext = new RsbEnggContext();

BusinessDetails bdetails = rsbcontext.businessdetails.Single(bd => bd.ID == id);



返回查看(bdetails);

}



在我的Index.cshtml中调用如: -



@model rsbenggmvc.Models.DBModel.BusinessDetails



@ Model.Clients



商业详情型号: -



名称空间rsbenggmvc.Models.DBModel

{

[表(TblBusinessDetails)) ]

公共类BusinessDetails

{

public int ID {get;组; }

public int Products {get;组; }

public int Clients {get;组; }

public int比较{get;组; }

public int Shares {get;组; }

}

}



RsbEnggContext.cs分类: -



名称空间rsbenggmvc.Models

{

公共类RsbEnggContext:DbContext

{

public DbSet< businessdetails> businessdetails {get;组; }

}

}



在Global.asax.cs课程中我添加了这个: -

Database.SetInitializer< rsbenggmvc.models.rsbenggcontext>(null);



最后我的Web.Config文件的coonectionstring为: -



< connectionstrings>

< add name =DefaultConnectionconnectionstring =Data Source = DEEPAK-PC\SQLEXPRESS; AttachDBFilename = | DataDirectory | aspnetdb.mdf; User Instance = true; Initial Catalog = rsbengg; Persistent Security Info = True; Integrated Security = SSPIprovidername =System.Data.SqlClient>

< add name =DatabaseEntitiesconnectionstring =metadata = res://*/ContactModel.csdl | res://*/ContactModel.ssdl | res://*/ContactModel.msl; provider = System.Data.SqlClient; provider connection string =data source = .\SQLEXPRESS; attachdbfilename = | DataDirectory | \Database.mdf; integrated security = True; user instance = True; MultipleActiveResultSets = True; App = EntityFrameworkprovidername = System.Data.EntityClient>





但我收到此错误: -



无法打开登录请求的数据库rsbenggmvc.Models.RsbEnggContext。登录失败。

用户'DEEPAK-PC\DEEPAK'登录失败。



请帮助....



Project Name :- rsbenggmvc
Context Class:- RsbEnggContext
Server Name :- DEEPAK-PC\DEEPAK
No Password.

What I have tried:

In My HomeController Index Action is:-

public ActionResult Index(int id)
{
RsbEnggContext rsbcontext = new RsbEnggContext();
BusinessDetails bdetails = rsbcontext.businessdetails.Single(bd => bd.ID == id);

return View(bdetails);
}

Calling in My Index.cshtml like:-

@model rsbenggmvc.Models.DBModel.BusinessDetails

@Model.Clients

BusinessDetails Model :-

namespace rsbenggmvc.Models.DBModel
{
[Table("TblBusinessDetails")]
public class BusinessDetails
{
public int ID { get; set; }
public int Products { get; set; }
public int Clients { get; set; }
public int Compares { get; set; }
public int Shares { get; set; }
}
}

RsbEnggContext.cs Class:-

namespace rsbenggmvc.Models
{
public class RsbEnggContext : DbContext
{
public DbSet<businessdetails> businessdetails { get; set; }
}
}

In Global.asax.cs Class I've added this :-
Database.SetInitializer<rsbenggmvc.models.rsbenggcontext>(null);

Finally my Web.Config file has coonectionstring as:-

<connectionstrings>
<add name="DefaultConnection" connectionstring="Data Source=DEEPAK-PC\SQLEXPRESS;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true; Initial Catalog=rsbengg;Persistent Security Info=True; Integrated Security=SSPI" providername="System.Data.SqlClient">
<add name="DatabaseEntities" connectionstring="metadata=res://*/ContactModel.csdl|res://*/ContactModel.ssdl|res://*/ContactModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework"" providername="System.Data.EntityClient">


But I get this error:-

Cannot open database "rsbenggmvc.Models.RsbEnggContext" requested by the login. The login failed.
Login failed for user 'DEEPAK-PC\DEEPAK'.

PLEASE HELP....

推荐答案

真的没有任何帮助。它归结为您阅读SQL Server安全性并了解它的工作原理以及如何设置它。你不可能有人只是在论坛帖子中给你一个模糊,它会神奇地工作。



错误非常清楚。 SQL Server不知道您是谁,因为您的帐户不在其自己的帐户列表中或在其信任的目录中。
There's really nothing to help with. It comes down to you reading up on SQL Server security and understanding how it works and how to set it up. There is no way you're going to have someone just give you a blurb in a forum post and it'll magically work.

The error is pretty clear. The SQL Server doesn't know who you are since your account isn't in either its own list of accounts or in a directory that it trusts.


这篇关于无法使用实体框架从ASP.NET MVC连接到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆