首頁 C# Connect MSSQL Use Entity Framework Repository Pattern
文章
Cancel

C# Connect MSSQL Use Entity Framework Repository Pattern

快速建立Repository Pattern的影片教學

詳見以下影片與GIT資源直接簡單建立檔案

Github Source Code

1.完成新增ADO.NET的操作 如「Entity Framework Code First if DataBase Exist-1

2.Creat New Class Name: IRepository.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace Entity_Framework_Repository_Pattern
{
    public interface IRepository<TEntity> where TEntity : class
    {
        TEntity Get(string id);
        IEnumerable<TEntity> GetAll();
        IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
        TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate);
        void Add(TEntity entity);
        void AddRange(IEnumerable<TEntity> entities);
        void Remove(TEntity entity);
        void RemoveRange(IEnumerable<TEntity> entities);
    }
}

3.Creat New Class Name: IMemberRepository.cs

1
2
3
4
5
6
7
namespace Entity_Framework_Repository_Pattern
{
    public interface IMemberRepository : IRepository<Member>
    {

    }
}

4.Creat New Class Name: Repository.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace Entity_Framework_Repository_Pattern
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly DbContext Context;

        public Repository(DbContext context)
        {
            Context = context;
        }

        public TEntity Get(string id)
        {
            return Context.Set<TEntity>().Find(id);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return Context.Set<TEntity>().ToList();
        }

        public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
        {
            return Context.Set<TEntity>().Where(predicate);
        }

        public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
        {
            return Context.Set<TEntity>().SingleOrDefault(predicate);
        }

        public void Add(TEntity entity)
        {
            Context.Set<TEntity>().Add(entity);
        }

        public void AddRange(IEnumerable<TEntity> entities)
        {
            Context.Set<TEntity>().AddRange(entities);
        }

        public void Remove(TEntity entity)
        {
            Context.Set<TEntity>().Remove(entity);
        }

        public void RemoveRange(IEnumerable<TEntity> entities)
        {
            Context.Set<TEntity>().RemoveRange(entities);
        }
    }
}

5.Creat New Class Name : MemberRepository.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace Entity_Framework_Repository_Pattern
{
    public class MemberRepository : Repository<Member>, IMemberRepository
    {
        public MemberRepository(Model1 context) : base(context)
        {
        }
        public Model1 Model1
        {
            get { return Context as Model1; }
        }
    }
}

6.Creat New Class Name : IUnitOfWork.cs

1
2
3
4
5
6
7
8
9
10
using System;

namespace Entity_Framework_Repository_Pattern
{
    public interface IUnitOfWork : IDisposable
    {
        IMemberRepository Member { get; }
        int Complete();
    }
}

7.Creat New Class Name : UnitOfWork.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System.Collections.Generic;
using System.Linq;

namespace Entity_Framework_Repository_Pattern
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly Model1 _context;
        public IMemberRepository Member { get; private set; }

        public UnitOfWork(Model1 context)
        {
            _context = context;
            Member = new MemberRepository(_context);
        }
        public int Complete()
        {
            return _context.SaveChanges();
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}

8.檔案結構化(可省略) Desktop View

9.Read MSSQL
Desktop View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity_Framework_Repository_Pattern
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (var unitOfWork = new  UnitOfWork(new Model1()))
            {
                var result = unitOfWork.Member.GetAll();
                foreach (var item in result)
                {
                    Console.WriteLine($"{item.UserID} {item.UserName} {item.UserEmail}");
                }
            }
            Console.ReadKey();
        }
    }
}

可以搭配的技術

本文由作者按照 CC BY 4.0 進行授權