0

I am struggling on how to connect existing database with existing data to my API project. And also how do I know if I'm already connected? I am using .NET Core 1.1 and my database is SQL Server 2012.

enter image description here

appsettings.json :

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
    "ConnectionStrings": {
      "DefaultConnection": "Server=.\\SQLEXPRESS;Database=;User Id=sa;Password=Passw0rd;MultipleActiveResultSets=True"
    }
  }
}

Startup.cs - ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    
    services.AddDbContext<DefaultConnection>(
    options => options.UseSqlServer(
    Configuration.GetConnectionString("DefaultConnection")));
}

I am following this tutorial but it seems that this creates new database. http://www.mithunvp.com/aspnet-core-web-api-entity-framework-core/

Would highly appreciate all your comments/solutions/suggestions.

4
  • 2
    Using a '.' is the same as using localhost - not sure if that is causing you issues? Is SQL installed on the machine you are trying to connect with? Either way, I would specify the server name instead of '.' Commented Sep 5, 2017 at 9:05
  • 2
    Change Server=.\\SQLEXPRESS to Server=COGSOL-LAP13... Read How to Ask, explain very explicitly what is and isn't working ("I am struggling" is not a proper problem description), and show what you have tried. Commented Sep 5, 2017 at 9:07
  • 2
    If you're not sure about your connection string format, check out connectionstrings.com. That site will be a huge help with ensuring that all the parameters are set properly. Commented Sep 6, 2017 at 0:19
  • @CodeCaster Apologies Sir. And thank you for your reply Commented Sep 7, 2017 at 0:50

1 Answer 1

3

You need to add the name of the database into that line :

 "DefaultConnection": "Server=COGSOL-LAP13;Database=DATABASENAME;UserId=sa;Password=Passw0rd;MultipleActiveResultSets=True"

And as CodeCaster comment says, you should use the name of the server too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.