How To Use Entityframework As The Data Source For The Master Detail View In Winui Datagrid

Sample date Updated on Sep 13, 2025
datagrid dbcontext detailsviewdatagrid entityframework master-detailsview masterdetailsview winui

In WinUI DataGrid (SfDataGrid), there was a way to utilize EntityFramework as the source for the MasterDetailsView. This process can be achieved by creating a class from DbContext and setting up SQLite as the database provider with the connection string. Additionally, the data for the MasterDetailsView is loaded and the SaveChanges method from DbContext is used to persist any changes made in the current context to the underlying database.

public class AppDbContext : DbContext
{
    public DbSet<Person> People { get; set; }

    public DbSet<Address> PeopleAddress { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Set up SQLite as the database provider with the connection string.
        string connectionString = "Data Source=PeopleDatabase.db";
        optionsBuilder.UseSqlite(connectionString);
    }
}

public class PeopleViewModel
{
    public readonly AppDbContext _context = new AppDbContext();
    public List<Person> GetPeople()
    {
        // Read data 
        return _context.People.ToList<Person>();
    }
    public List<Address> GetAddresses()
    {
        return _context.PeopleAddress.ToList();
    }

    public void AddPerson(Person person)
    {
        // Add a new person to the People DbSet
        _context.People.Add(person);
        // Save changes to the database
    } 
}
Up arrow