简体   繁体   English

如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库?

[英]How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework?

I am using: 我在用:

  • VMware V10.0.1. VMware V10.0.1。
  • Visual Studio Ultimate 2013. Visual Studio Ultimate 2013。
  • SQL Server 2012. SQL Server 2012。

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.

相关问题 ASP.NET MVC - 如何首先使用实体框架模型/数据库部署到 Azure? - ASP.NET MVC - How to Deploy to Azure using Entity Framework Model/Database First? ASP.NET MVC 4实体框架数据库第一个存储库 - ASP.NET MVC 4 Entity Framework Database First Repository 代码优先实体框架Asp.net MVC - Code First Entity Framework Asp.net MVC 具有实体框架代码优先导航属性的ASP.Net MVC - ASP.Net MVC with Entity Framework Code First Navigation Property 首先在ASP.net MVC实体框架代码中创建外键关系 - Create Foreign Key Relationship in ASP.net MVC Entity Framework Code First 使用实体框架的代码优先方法在ASP.NET MVC应用程序中缺少外键关系 - Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework 在ASP.NET MVC项目中使用实体框架代码优先时登录失败错误 - Login failed error when using Entity Framework code-first in ASP.NET MVC project 使用实体框架代码优先的ASP.NET MVC 3 Web应用程序上的列名ProveedoresID和UsuarioID无效 - Invalid column name ProveedoresID and UsuarioID on ASP.NET MVC 3 web app using Entity Framework Code First 如何首先在ASP.NET MVC中将Identity与Entity Framework数据库一起使用 - How to use Identity with Entity Framework database first in ASP.NET MVC 如何在 ubuntu 桌面上使用 Entity Framework Core 将 asp.net core MVC 项目连接到本地数据库 - how to connect asp.net core MVC project to local DB using Entity Framework Core on ubuntu desktop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM