Bojan Grujic

Bojan Grujic

  • NA
  • 80
  • 756

MVVM Database CRUD EF Core

Feb 18 2020 2:07 AM
Hello everybody,
I am absolute begginer in MVVM pattern and having real trouble to figure out how it works. I learned some basics, but i am stuck at ViewModel. In model i placed entity class and database context class.
Entity:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Test.Model
{
[Table("Proizvodi")]
public class Proizvodi
{
[Key] public int Id { get; set; }
[Required] public string Sifra { get; set; }
[Required] public string Naziv { get; set; }
}
}
Database context class:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Test.Model
{
public class BazaContext : DbContext
{
public virtual DbSet<Proizvodi> Proizvodi { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;");
}
}
}
And in App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
using(var ctx=new Model.BazaContext())
{
ctx.Database.Migrate();
}
}
 
I want to create datagrid and a button in window, bind to viewmodel and use command for button to perform CRUD operations and save changes to database. How to do that?
I watched many tutorials but i still dont get it.
 

Answers (1)