An Example of a Modern Feature-Rich Entity Framework using SQLite DB

Leave a Comment

Modern features like C#'s async/await will be employed, along with an entity framework and SQLite database to handle user addition and fetching.Net Core software. Let's install the Entity framework and SQLite Database packages from Manage NuGet Packages and construct a basic.NET Core application in Visual Studio 2022.

Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Tools

Asynchronous methods can help to perform tasks without blocking the running threads. Asynchronous functions generally contain the await expression and Task keyword Task<T> with returns.

Example

static async Task Main(string[] args)
    {
        Console.WriteLine("Fetching data...");
        //
    }
static async Task<string> FetchDataAsync(string url)
    {
        using (HttpClient client = new HttpClient())
        {

            string response = await client.GetStringAsync(url);
            return response;
        }
    }

Create a model Class file for table structure and execute application SQLite DB file will create

Example Model Class

public class ItemContext : DbContext
{
    public ItemContext() { }

    public DbSet<Item> Products { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=Item.db");
    }
}

public class Item
  {

      public int Id { get; set; }
      public string Name { get; set; }
      public decimal Price { get; set; }
  }
public static async Task  ClassDBExample()
{
    using (var db = new ItemContext())
    {
        await db.Database.EnsureCreatedAsync();
        var product = new Item { Name = "PC", Price = 899.99m};
        db.Products.Add(product);
        await db.SaveChangesAsync();
        var products = await db.Products.ToListAsync();
        foreach (var item in products)
        {
            Console.WriteLine($"ID: {item.Id}, Name: {item.Name}, Price: {item.Price}");
        }

        product.Price = 899.99m;
        await db.SaveChangesAsync();

        db.Products.Remove(product);
        await db.SaveChangesAsync();
    }
 }

Output


 

Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 7.0.10 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment