简体   繁体   中英

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.
  • Visual Studio Ultimate 2013.
  • 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.

The database is really simple while it has only one table (User) with the following field (UserId, Username, Name, Surname). In order to build it I created the following class in 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:

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:

<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. 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.

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

I changed the providerName to "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. Additional information: Unable to find the requested .Net Framework Data Provider. It may not be installed.

@jebar8 @DominicZukiewicy

Isn't it

public Context() : base("UserDBConnectionString") {}

instead of

public Context() : base("name=UserDBConnectionString") {}

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM