How to use Repository pattern using Database first approach in entity framework
Asked Answered
P

1

7

How to use Repository pattern using Database first approach in entity framework.I got some idea while going through resources available on internet but for real time applications I am not sure how to implement repository pattern on the auto generated classes from the DB first approach.

I have already gone through some links in SO but I did not get any clear idea.I am new to this one. Thanks in Advance.

Perspex answered 21/5, 2015 at 13:11 Comment(1)
Can you explain clearer what it is you want? Also please show your current code and what it's output is.Guadalupeguadeloupe
T
4

The code generation tool will only modify the classes that are mapped to the XML files. You have a couple of options:

1) You can extend the mapped classes by using partial classes. Partial classes will not be modified by the automated tool when updating the code with the tool.

2) You can also handle data annotations and entity configuration to a Configuration file, just be cautious because in some cases they may conflict. Down below a snippet:

public class YourClassConfiguration : EntityTypeConfiguration<YourClass>
{
    public YourClassConfiguration()
    {
        ToTable("YourTable");
        HasKey(e => e.Property1);
        Property(e => e.Property1).HasColumnName("MyName").HasMaxLength(30);
    }
}

I was working with this approach and honestly, I would suggest you moving your implementation to a Code First approach as I did. In my personal opinion, it's a pain maintaining and fixing problems when the designer decides to create duplicate keys or not updating properly the XML file as it happen to me more than once. The good news is that you can avoid migrations and some of the features used by Code First and preserving the DB structure as is. Everything can be configured. I can tell you more if you are interested.

Anyhow, I am also attaching you a simple code snippet of a GenericRepository pattern that you may find useful. I will also strongly suggest using dependecy Injection in your implementation (Required by the GenericRepository pattern to resolve dependencies). I would recommend Autofac. It's very stable and has great support.

    public class EntityRepository<T> : IRepository<T>, IDisposable where T
                                    : class, IEntity
    {

        private readonly DbSet<T> dbset;
        private readonly DbContext _context;
        private readonly bool _lazyLoadingEnabled = true;


        public EntityRepository(DbContext context, bool lazyLoadingEnabledEnabled)
        : this(context)
        {
            _lazyLoadingEnabled = lazyLoadingEnabledEnabled;
        }

        public EntityRepository(DbContext context)
        {
            _context = context;
            _context.Configuration.LazyLoadingEnabled = _lazyLoadingEnabled;
            dbset = context.Set<T>();
        }

        public void Add(T entity)
        {
            dbset.Add(entity);
            _context.SaveChanges();
        }

        public void Update(T entity)
        {
            var originalValues = FindOne(x => x.Id == entity.Id);
            _context.Entry(originalValues).CurrentValues.SetValues(entity);
            _context.SaveChanges();
        }

        public void Remove(T entity)
        {
            dbset.Remove(entity);
            _context.SaveChanges();
        }

        public List<T> Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return dbset.Where(predicate).ToList();
        }

        public T FindOne(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return dbset.FirstOrDefault(predicate);
        }

        public List<T> FindAll()
        {
            return dbset.ToList();
        }
    }

The interface is quite simple:

    public interface IRepository<T>
                    where T : class, IEntity
    {
        void Add(T entity);
        void Update(T entity);
        void Remove(T entity);
        T FindOne(Expression<Func<T, bool>> predicate);
        List<T> Find(Expression<Func<T, bool>> predicate);
        List<T> FindAll();
    }

Apply the interface to the partial classes you created to extend the database first classes and you will be able to query those entities within the repository. You can also add Properties to the repository interface to have those properties visible and add common functionality of searching using common properties such as Id, name and so on if applicable.

I hope it helps,
Carlos

Timbuktu answered 21/5, 2015 at 14:5 Comment(4)
what I understood is that you are saying we can apply the repository pattern to the model classes generated automatically. Is it the same way we do with the code first approach.In code first approach we have POCO classes on which we apply repository pattern. So shall we consider those POCO to be the auto generated classes from the DB first approach and proceed the same way as we do in code first approach. Because I found more examples in internet which uses code first for implementing repository pattern. Can you show the same thing using database first(edmx diagram).Perspex
HI Rakesh, That is correct. Depending on the version of visual studio there are different ways you can generate those objects. For Visual Studio 2010 (EF 4.x) you need to run a custom tool. Visual Studio 2013 already generates the pocos in a TT file that is not visible from within the solution explorer. I believe that in VS 2013 you can also configure the classes to be deployed to a target assembly. This one I am not a 100% sure. Check the following link: msdn.microsoft.com/en-us/data/jj613116.aspxTimbuktu
Correction. The TT file can be seen from within the solution explorer in VS 2013. Expand the EDMX, then epand the TT file and you will find a nested "Context.cs" file that contains the generated context. You can navigate from there to the definition of the pocos so you get an idea of how do they work. Check this other link: blogs.msdn.com/b/adonet/archive/2010/01/25/…Timbuktu
Thanks Charles for your answers now I got some idea.Perspex

© 2022 - 2024 — McMap. All rights reserved.