-1

I'm trying to create a very simple C# program for insert the data.

Here is the service file :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DataAccess;
using DataAccess.UoW;
using Model;

namespace ClassLibrary1
{
    public class Service
    {
        private readonly Unit _uow;

        public Service()
        {
            _uow = new Unit();
        }

        public bool CreateEmp(Mdl insertEntity)
        {
            var config = new MapperConfiguration(cfg =>
                             {
                                 cfg.CreateMap<Mdl, Table_1>();
                             });

            IMapper mapper = config.CreateMapper();

            var Empinsert = mapper.Map<Mdl, Table_1>(insertEntity);
            _uow.Register.Insert(Empinsert);

            _uow.Save();  //this line shows error

            return false;
        }
    }
}

Unit of Work:

using DataAccess.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.UoW
{
    public class Unit
    {
        private guruEntities _context = null;
        private Repository<Table_1> _register;

        public Unit()
        {
            _context = new guruEntities();
        }
        public Repository<Table_1> Register
        {
            get
            {
                if (this._register == null)
                    this._register = new Repository<Table_1>(_context);
                return _register;
            }
        }
    }
}

And this is the error I get :

C# 'Unit' does not contain a definition for 'Save' and no accessible extension method 'Save' accepting a first argument of type 'Unit' could be found (are you missing a using directive or an assembly reference?)

1 Answers1

1

You have to add save method in your Unit Class like this

    public void Save()
    {
        context.SaveChanges();
    }

for better understand you can refer Unitofwork class from below link

https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

Hardik Leuwa
  • 3,282
  • 3
  • 14
  • 28