How to use EntityFramework as the data source for the Master-Detail view in WinUI DataGrid?
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
}
}
Conclusion
I hope you enjoyed learning about How to use EntityFramework as the data source for the Master-Detail view in WinUI DataGrid.
You can refer to our WinUI DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinUI DataGrid documentation to understand how to present and manipulate data.
For current customers, you can check out our WinUI components from the Licence and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinUI DataGrid and other WinUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!