asp.net mvc与sql server [英] asp.net mvc with sql server
问题描述
请让我知道如何使用sql server制作asp.net mvc ..我的意思是实体框架,我不应该通过导入数据库文件来使用loacl数据库。所以我为sql server写连接字符串..通过这个我想用DbContext做数据库操作..
这里是我的连接string
< add name =MyConnectionconnectionstring =Data Source = v-rrd2005; User ID = sa; Password = pa55w0rd !;初始目录= TrainingDB;集成安全性=真providername =System.Data.SqlClient>
但是我无法连接sql server ..
这里是context.cs文件
please let me know how make asp.net mvc with sql server.. i mean with out entity framework, and i am not supposed to use loacl database by importing database file. so i write connection string for sql server.. by this i want to do database opearations using DbContext..
here is my connection string
<add name="MyConnection" connectionstring="Data Source=v-rrd2005; User ID=sa;Password=pa55w0rd!;Initial Catalog=TrainingDB;Integrated Security=True" providername="System.Data.SqlClient">
but i am unable to connect sql server..
here is context.cs file
public partial class MyConnection : DbContext
{
public MyConnection()
: base("name=MyConnection")
{
// Database.SetInitializer<DbContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// throw new UnintentionalCodeFirstException();
}
public DbSet<EmployeeInfo> TrainingDB { get; set; }
}
这里我的EmployeeInfo是表名,TrainingDB是数据库名
我的控制器页面
< br $>
here my EmployeeInfo is table name and TrainingDB is database name
my contorler page
public class GetEmpInfoController : ApiController
{
private MyConnection db = new MyConnection();
// GET api/EmployeeInfoAPI
public IEnumerable<employeeinfo> GetEmployeeInfoes()
{
return db.TrainingDB.AsEnumerable();
}
// GET api/EmployeeInfoAPI/5
public EmployeeInfo GetEmployeeInfo(int id)
{
EmployeeInfo employeeinfo = db.TrainingDB.Find(id);
if (employeeinfo == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return employeeinfo;
}
}
我无法连接到我的sqlserver ..
im unable to make connection to my sqlserver..
推荐答案
要使用Entity Framework,可以在项目中添加一个edmx,它可以构建您使用的类。它还将连接字符串插入到web.config。
To use Entity Framework, you add an edmx to your project, which builds the classes you use. It also inserts the connection string in to your web.config.
这篇关于asp.net mvc与sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!