My C# WPF (Bike Store) database interface application works with the SQL Server LocalDB database (target framework is .NET 6.0). The idea is that when installing the application on users computers, they can use the functionality associated with the database (99% of the work).
The functionality includes basic CRUD operations and the display of tables (there are two of them) in a DataGrid. The database itself is connected using Scaffold-DbContext via NUGET Package Manager, as a Local DB. As a result, there are the following classes: Order (table), Warehouse (table), BikestoriesContext (DbContext class).
When running from Visual Studio 2022, the project works perfectly, but when I run an .exe file created using Advanced Installer and try to perform some actions with the database, the program just freezes and then crashes. The very essence of the problem is that I do not understand at all how to add the database files themselves to the project or how to properly bind them with project so that when installed on the user's computer, the project can function correctly.
I tried to create Setup Wizard using programs like Inno Setup, Advanced Installer and via Visual studio Setup Project, but I'm writing this here, so nothing worked. When I tried to place the BIkeStores.mdf and BIkeStores.Idf files inside project and it ended in the same way (nothing works). Can someone please explain this to me? I'm complete newbie btw.
Order.cs
using System;
using System.Collections.Generic;
namespace BikeStore;
public partial class Order
{
public int Id { get; set; }
public string? CustomerName { get; set; }
public string? CustomerSurname { get; set; }
public string? PhoneNumber { get; set; }
public string? Email { get; set; }
public string? ResidentialAddress { get; set; }
public string? BikeCategory { get; set; }
public string? BikeBrand { get; set; }
public string? BikeModel { get; set; }
public string? EmployeeName { get; set; }
public string? EmployeeSurname { get; set; }
}
Warehouse.cs
using System;
using System.Collections.Generic;
namespace BikeStore;
public partial class Warehouse
{
public int Id { get; set; }
public string? ItemName { get; set; }
public string? ItemBrand { get; set; }
public string? ItemModel { get; set; }
public int? ItemQuantity { get; set; }
public string? DateOfReceipt { get; set; }
public string? WarehouseName { get; set; }
public string? EmployeeName { get; set; }
public string? EmployeeSurname { get; set; }
}
BikeStoresContext.cs
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace BikeStore;
public partial class BikeStoresContext : DbContext
{
public BikeStoresContext()
{
}
public DbSet<Order> Orders { get; set; }
public DbSet<Warehouse> Warehouses { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BIkeStores;Integrated Security=True;Trust Server Certificate=True;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>(entity =>
{
entity.HasNoKey();
entity.Property(e => e.BikeBrand)
.IsUnicode(false)
.HasColumnName("bike_brand");
entity.Property(e => e.BikeCategory)
.IsUnicode(false)
.HasColumnName("bike_category");
entity.Property(e => e.BikeModel)
.IsUnicode(false)
.HasColumnName("bike_model");
entity.Property(e => e.CustomerName)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("customer_name");
entity.Property(e => e.CustomerSurname)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("customer_surname");
entity.Property(e => e.Email)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("email");
entity.Property(e => e.EmployeeName)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("employee_name");
entity.Property(e => e.EmployeeSurname)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("employee_surname");
entity.Property(e => e.Id)
.ValueGeneratedOnAdd()
.HasColumnName("id");
entity.Property(e => e.PhoneNumber)
.HasMaxLength(100)
.IsUnicode(false)
.HasColumnName("phone_number");
entity.Property(e => e.ResidentialAddress)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("residential_address");
});
modelBuilder.Entity<Warehouse>(entity =>
{
entity
.HasNoKey()
.ToTable("Warehouse");
entity.Property(e => e.DateOfReceipt)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("date_of_receipt");
entity.Property(e => e.EmployeeName)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("employee_name");
entity.Property(e => e.EmployeeSurname)
.HasMaxLength(255)
.IsUnicode(false)
.HasColumnName("employee_surname");
entity.Property(e => e.Id)
.ValueGeneratedOnAdd()
.HasColumnName("id");
entity.Property(e => e.ItemBrand)
.IsUnicode(false)
.HasColumnName("item_brand");
entity.Property(e => e.ItemModel)
.IsUnicode(false)
.HasColumnName("item_model");
entity.Property(e => e.ItemName)
.IsUnicode(false)
.HasColumnName("item_name");
entity.Property(e => e.ItemQuantity).HasColumnName("item_quantity");
entity.Property(e => e.WarehouseName)
.IsUnicode(false)
.HasColumnName("warehouse_name");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
In order to load data to datagrid there are following lines in the constructor:
public List<Order> MyOrders { get; set; }
public ObserveOrders()
{
InitializeComponent();
using (BikeStoresContext _context = new BikeStoresContext())
{
MyOrders = _context.Orders.ToList();
}
ordersDataGrid.ItemsSource = MyOrders;
}
And this connection string is used pretty much in every file of the project:
SqlConnection con = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BIkeStores;Integrated Security=True;Trust Server Certificate=True;");
I was trying to create a Setup Wizard for the project, but ended up without understanding of how to make the app work on someone's other computer with DB. I was using the following tools Inno Setup, Visual Studio Setup project, Advanced Installer. Can anyone please explain how?