Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create platform and permissions neutral path for blog #3298

Merged
merged 3 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions samples/core/GetStarted/Model.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace EFGetStarted
Expand All @@ -8,10 +9,19 @@ public class BloggingContext : DbContext
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

// The following configures EF to create a Sqlite database file as `C:\blogging.db`.
// For Mac or Linux, change this to `/tmp/blogging.db` or any other absolute path.
public string DbPath { get; private set; }

public BloggingContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = $"{path}{System.IO.Path.DirectorySeparatorChar}blogging.db";
}

// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite(@"Data Source=C:\blogging.db");
=> options.UseSqlite($"Data Source={DbPath}");
}

public class Blog
Expand Down
3 changes: 2 additions & 1 deletion samples/core/GetStarted/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ private static void Main()
using (var db = new BloggingContext())
{
// Note: This sample requires the database to be created before running.

Console.WriteLine($"Database path: {db.DbPath}.");

// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
Expand Down