[英]How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework?
I am using: 我在用:
I created an ASP.NET MVC 4 Internet Application and I want to build a database by using the Code First technique from Entity Framework. 我创建了一个ASP.NET MVC 4 Internet应用程序,我想使用实体框架中的“代码优先”技术来构建数据库。
The database is really simple while it has only one table (User) with the following field (UserId, Username, Name, Surname). 数据库只有一个表(用户),其中具有以下字段(用户ID,用户名,名称,姓氏),因此非常简单。 In order to build it I created the following class in C# :
为了构建它,我在C#中创建了以下类:
namespace MyQuestionProject.Models
{
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public User()
{
}
}
}
I also created my Context class which is the one shown below: 我还创建了Context类,如下所示:
using System.Data.Entity;
namespace MyQuestionProject.Models
{
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public Context() : base("name=UserDBConnectionString")
{
}
}
}
I have changed also my connection string in "Web.config" file and the changed connection string is the following: 我还更改了“ Web.config”文件中的连接字符串,更改后的连接字符串如下:
<connectionStrings>
<add name="UserDBConnectionString" connectionString="Data Source=stavra-dev;Server=STAVRA-DEV;Database=UserDB;Trusted_Connection=True;" providerName="System.Data.EntityClient;"/>
</connectionStrings>
and in order to be able to observe that the connection is really established, I added some code which actually adds a record to the database. 并且为了能够观察到该连接已真正建立,我添加了一些代码,实际上将记录添加到了数据库中。 I used razor in my "index.cshtml" in my "home" folder.
我在“ home”文件夹的“ index.cshtml”中使用了razor。 The code is the one below:
代码如下:
@using MyQuestionProject.Models
@{
ViewBag.Title = "Home Page";
}
@{
using (var ctx = new Context())
{
User stud = new User() { UserId = 1, Username = "StaVra", Name = "Stavros", Surname = "Vrakas"};
ctx.Users.Add(stud);
ctx.SaveChanges();
}
}
The previous code is correct, but the connection string seems that it does not work. 先前的代码正确,但是连接字符串似乎不起作用。
I followed the information regarding Code First in >> this << Web site and I found information about SQL Server 2012 Connection String in >> this << site. 我就跟着一码>>中的信息本 <<网站,我发现有关SQL Server 2012的连接字符串>>信息本 <<网站。
It has been some days I am trying to figure out how I can manage to make that work and I will appreciate any assistance that can be provided on this topic. 几天以来,我一直在努力弄清如何进行这项工作,对于在此主题上可以提供的任何帮助,我将不胜感激。
Any suggestion or idea is more than welcome. 任何建议或想法都非常受欢迎。
The Entity Framework Version is 5.0.0.0 and Runtime Version v4.0.30319 实体框架版本为5.0.0.0,运行时版本为v4.0.30319
I changed the providerName to "System.Data.SqlClient" 我将providerName更改为“ System.Data.SqlClient”
The error that occures is the following: 发生的错误如下:
An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code. System.Data.dll中发生类型'System.ArgumentException'的异常,但未在用户代码中处理。 Additional information: Unable to find the requested .Net Framework Data Provider.
附加信息:无法找到请求的.Net Framework数据提供程序。 It may not be installed.
它可能没有安装。
@jebar8 @DominicZukiewicy @ jebar8 @DominicZukiewicy
Isn't it 是不是
public Context() : base("UserDBConnectionString") {}
instead of 代替
public Context() : base("name=UserDBConnectionString") {}
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.