using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace WpfApp6.Models;
public partial class Product
[Key]
public int IdProduct { get; set; }
public string NameOfProduct { get; set; } = null!;
public virtual ICollection<Purchase> Purchases { get; set; } = new List<Purchase>();
```}
internal class ProductRepository : DbContext
{
```typescript
public DbSet<Product> Products { get; set; } = null!;
public ProductRepository() => Database.EnsureCreated();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("server=127.0.0.1;port=3306;user=******;password=******;database=Shop",
ServerVersion.AutoDetect("server=127.0.0.1;port=3306;user=******;password=******;database=Shop"));
}
public List<Product> GetProducts()
{
return Products.ToList();
}
public partial class ShopContext : DbContext
public ShopContext()
{
}
public ShopContext(DbContextOptions<ShopContext> options)
: base(options)
{
}
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Purchase> Purchases { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseMySql("server=127.0.0.1;port=3306;user=******;password=******;database=Shop", ServerVersion.Parse("9.0.0-mysql"));
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.UseCollation("utf8mb4_0900_ai_ci")
.HasCharSet("utf8mb4");
modelBuilder.Entity<Product>(entity =>
{
entity.HasKey(e => e.IdProduct).HasName("PRIMARY");
entity.ToTable("product");
entity.Property(e => e.IdProduct).HasColumnName("idProduct");
entity.Property(e => e.NameOfProduct)
.HasMaxLength(45)
.HasColumnName("nameOfProduct");
});
modelBuilder.Entity<Purchase>(entity =>
{
entity.HasKey(e => e.IdPurchase).HasName("PRIMARY");
entity.ToTable("purchase");
entity.HasIndex(e => e.IdProduct, "idProduct_idx");
entity.Property(e => e.IdPurchase).HasColumnName("idPurchase");
entity.Property(e => e.DateOfPurchase).HasColumnName("dateOfPurchase");
entity.Property(e => e.IdProduct).HasColumnName("idProduct");
entity.Property(e => e.QuantityOfPurchases).HasColumnName("quantityOfPurchases");
entity.Property(e => e.TotalCost)
.HasPrecision(10, 2)
.HasColumnName("totalCost");
entity.HasOne(d => d.IdProductNavigation).WithMany(p => p.Purchases)
.HasForeignKey(d => d.IdProduct)
.HasConstraintName("idProduct");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
ProductRepository productRepository = new() ;
List <Product> products=productRepository.GetProducts();
}
private void Button_AddProducts(object sender, RoutedEventArgs e)
{
DirectoryOfProducts directoryOfProducts = new DirectoryOfProducts();
directoryOfProducts.Show();
}
Hello. I'm writing an application in wpf, for the first time I connected Entity Framework and MySQL. I created the database myself, after which Entity Framework built the classes itself. I wanted to display product values from a table. But an error appears - there is no primary key. Where did I make a mistake? Why does this happen and how to correctly register CRUD operations. This is my first time practicing Entity Framework