Skip to content

Instantly share code, notes, and snippets.

@AliKhadivi
Forked from garymedina/LiteDB CRUD ASP.NET
Last active July 11, 2022 11:41
Show Gist options
  • Save AliKhadivi/9c391f867e04e69dd416065be4e7ede2 to your computer and use it in GitHub Desktop.
Save AliKhadivi/9c391f867e04e69dd416065be4e7ede2 to your computer and use it in GitHub Desktop.
LiteDB Crud operations and path for use in ASP.NET
//ASP.NET Database path
var path = Server.MapPath("~/App_Data/site.db");
using (var db = new LiteDatabase(path))
{
//Sets or Creates a Table in DB
var bottles = db.GetCollection<Bottle>("Bottles");
//Creates a new bottle object and saves in db
var bottle = new Bottle
{
Name = "Something",
Price = 600
};
bottles.Insert(bottle);
//Get all the bottles to list
List<Bottle> _list = bottles.FindAll().ToList();
//Get a bottle by Id and update it
Bottle _bottle = bottles.FindOne(b => b.Id == 4);
_bottle.Name = "Kettle One";
bottles.Update(_bottle);
//Delete by Id
bottles.Delete(b=>b.Id == 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment