0

On creating an ASP.NET MVC project on Visual Studio 2017 community version, the project does not contain an app.db file(and doesn't use SQLite by default). On the other hand while creating the same type of project through the dotnet cli by using the following command: dotnet new mvc --auth Individual -o myMvcApp creates an app.db file and uses the SQLite database by default.

Is there any way in which I can add an SQLite database to the existing solution(project) in Visual Studio? How can I make this process default?

3
  • 1
    why not just create it from the command line then open it in VS? I think they make the command line project templates more cross platform friendly whereas VS for windows they use mssql localdb by default. after all they want you to use Azure Commented May 15, 2018 at 11:52
  • Yeah that's true but then I am not able to run it in Visual Studio. It is throwing some exceptions. Commented May 15, 2018 at 11:59
  • a project created from dotnet new command should be able to run in VS, if you get unexpected errors I would ask about that and post the error details Commented May 15, 2018 at 12:46

1 Answer 1

1

Visual Studio default template comes with SQL Server Connection string and Configure Services, You can configure Startup.cs and appsettings.json file to use SQLite DB.

In appsettings.json modify ConnectionStrings like below.

"ConnectionStrings": {
    "DefaultConnection": "DataSource=app.db"
 },

Now, In Startup.cs under ConfigureServices method modify services.AddDbContext to use UseSqlite like below.

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

Hope this will help!

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.