• 1399/05/15

مشکل هنگام اضافه کردن سطر جدید به دیتابیس در Entity Framework :

سلام دوستان. خسته نباشید. بنده در حال نوشتن پروژه‌ی حسابداری هستم که استاد مدائنی به عنوان پروژه‌ی عملی تو این دوره (سی‌شارپ پیشرفته) آموزش داده بودن. یه مشکلی هست که وقتی متد Insert رو که درون Repository من قرار داره، اجرا میکنم: اگر متد SaveChanges رو از خود Context صدا بزنم، سطر جدیدی به دیتابیس اضافه نمیشه ولی اگر متد SaveChanges رو در خود متد Insert که درون Repository قرار بدم و با هر بار Insert اجرا بشه، سطر به دیتابیس بدون مشکلی اضافه میشه؛ اما خوب من نمیخوام این طوری باشه چون امکان داره من ۱۰ تا Insert داشته باشم و اگر برای هر کدوم یه بار SaveChanges کنم، طبیعتا درست نیست و سرعت برنامه پایین میاد. به نظرتون مشکل از کجاست؟

// Context

using DataLayer.Repositories;
using DataLayer.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataLayer.Context
{
    public class Context : IDisposable
    {
        AccountingEntities db = new AccountingEntities();
        private AccountingRepository _accountingRepository;
  
        public AccountingRepository Repository
        {
            get
            {
                if (_accountingRepository == null)
                {
                    _accountingRepository = new AccountingRepository(db);
                }
                return _accountingRepository;
            }
        }

        public void Dispose()
        {
            db.Dispose();
        }

        public void SaveChanges()
        {
            db.SaveChanges();
        }
    }
}
______________________________________________________________________________
// Repository
using DataLayer.Repositories;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataLayer.Services
{
    public class AccountingRepository : IAccountingRepository
    {
        private AccountingEntities _db = new AccountingEntities();

        public AccountingRepository(AccountingEntities context)
        {
            if (_db == null)
            {
                _db = context;
            }
        }

        public List<Customer> GetAllCustomers()
        {
            return _db.Customers.ToList();
        }

        public IEnumerable<Customer> GetCusstomersByFilter(string parameter)
        {
            return _db.Customers.Where(customer => customer.Name.Contains(parameter) || customer.Email.Contains(parameter)).ToList();
        }

        public Customer GetCustomerById(int customerId)
        {
            return _db.Customers.Find(customerId);
        }

        public bool InsertCustomer(Customer customer)
        {
            try
            {
                _db.Customers.Add(customer);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateCustomer(int id, string name, string email)
        {
            try
            {
                Customer newCustomer = GetCustomerById(id);
                newCustomer.Name = name;
                newCustomer.Email = email;
                _db.SaveChanges();
                return true;

            }
            catch
            {
                return false;
            }
        }

        public bool DeleteCustomer(Customer customer)
        {
            try
            {
                _db.Customers.Remove(customer);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteCustomer(int customerId)
        {
            try
            {
                Customer customer = new Customer();
                customer = _db.Customers.Find(customerId);
                DeleteCustomer(customer);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
logo-samandehi