JackXandar
JackXandar

Reputation: 543

Entity Framework isn't saving data to database in WPF

I'm developing a POS in WPF. For CRUD operations, I'm using Entity Framework.

  1. Created a WPF View ProductADD Product Add View Snap
  2. Created a Class ProductController in Controller Folder
  3. Made object of Entity Framework in ProductController Class ProductController Calss Snap

  4. Created a method: SaveProduct(Product product) which is taking product object as argument and saving it to database using EF.

  5. And From Xaml.Cs I'm calling ProductController Class's Saveproduct method and sending the new product data to it.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
         ProductController pc = new ProductController();
         PRODUCT product = new PRODUCT();
    
         product.PRODUCT_NAME = Product_Name.Text.ToString();
         product.UNITPRICE = Convert.ToInt32(Unit_Price.Text.ToString());
         product.CATEGORY_Id = 1;
         pc.SaveProduct(product);
    
         MessageBox.Show("Product Added Successfully");
    
         this.Close();
    }
    

And in ProductController the following code is updating the database

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PizzaLounge.Models;

namespace PizzaLounge.Controllers 
{
	public class ProductController 
	{
		PizzaLoungeEntities db = new PizzaLoungeEntities();
		public void SaveProduct(PRODUCT product) 
		{
			db.PRODUCTs.Add(product);
			db.SaveChanges();
		}
	}
}

  1. The code executes successfully but it doesn't save the product in database. P.S. I have used db.savechanges().

Am I missing something or using wrong approach to update database?

Upvotes: 3

Views: 2365

Answers (3)

mohsen
mohsen

Reputation: 1806

Because you are using mdf file attached to your project so your problem is like this question

Attaching database to my project

You are saving data to database that is in bin\debug folders ,and then you see the mdf file that is in your project folder and you don't see the data .

change your connection string from DataDirectory to absolute path to the project database file. When deploying, just change it back to |DataDirectory|

Upvotes: 1

mark_h
mark_h

Reputation: 5477

You are probably using |DataDirectory| in your connection string. If debugging in Visual Studio, the database you are using is in the bin/debug folder. Unfortunately if you look at the db through Server Explorer it has a different connection string so you don't see the changes.

Also if the database property "Copy to Output directory" is set to Copy Always then every time you debug you will overwrite your db and you won't see the data you added. You can check if this is happening by using a new db context in the same debug session where you add the records. If the new context can get the records from the db then you know they must be being written (as well as the other checks listed in the comments)

This can be fixed by changing Copy To Output Directory to Never Copy or Copy If Newer.

Upvotes: 2

Baldurs
Baldurs

Reputation: 174

How did you checked that the products haven't been added, from db.PRODUCTs or from the Database Explorer?

Maybe you just need to dispose your context, change your ProductController for something like:

using(var db = new PizzaLoungeEntities()){
   db.PRODUCTs.Add(product);
   db.SaveChanges();
}

Or to dispose ProductController after you finish to use it.

Upvotes: -1

Related Questions