zoukankan      html  css  js  c++  java
  • 分享一个老外写的Entityframework的知识库

    /// <summary>
        /// Repository base class used with DbContext
        /// Via http://dotnetspeak.com/index.php/2011/03/repository-pattern-with-entity-framework/
        /// </summary>
        /// <typeparam name="TContext">Type of DdContext that this repositiory operates on</typeparam>
        public class EFRepository<TContext> : IDisposable
            where TContext : DbContext, IObjectContextAdapter, new()
        {
            private TContext context;
    
            public TContext Context
            {
                get { return context; }
            }
    
            private EFRepository()
            {
    
            }
    
            /// <summary>
            /// Create new instance of repository
            /// </summary>
            /// <param name="connecstionStringName">Connection string name from .config file</param>
            public EFRepository(string connecstionStringName)
            {
                context = new TContext();
                context.Database.Connection.ConnectionString =
                    ConfigurationManager.ConnectionStrings[connecstionStringName].ConnectionString;
            }
    
            /// <summary>
            /// Dipose repository
            /// </summary>
            public void Dispose()
            {
                if (context != null)
                {
                    context.Dispose();
                    context = null;
                }
            }
    
    
            /// <summary>
            /// Select data from database
            /// </summary>
            /// <typeparam name="TItem">Type of data to select</typeparam>
            /// <returns></returns>
            public IQueryable<TItem> Select<TItem>()
               where TItem : class, new()
            {
                PropertyInfo property = GetDbSet(typeof(TItem));
    
                DbSet<TItem> set = property.GetValue(context, null) as DbSet<TItem>;
    
                return set;
            }
    
            /// <summary>
            /// Insert new item into database
            /// </summary>
            /// <typeparam name="TItem">Type of item to insert</typeparam>
            /// <param name="item">Item to insert</param>
            /// <returns>Inserted item</returns>
            public TItem Insert<TItem>(TItem item)
                where TItem : class, new()
            {
                DbSet<TItem> set = GetDbSet(typeof(TItem)).GetValue(context, null) as DbSet<TItem>;
                set.Add(item);
                context.SaveChanges();
                return item;
            }
    
            /// <summary>
            /// Update na item
            /// </summary>
            /// <typeparam name="TItem">Type of item to update</typeparam>
            /// <param name="item">Item to update</param>
            /// <returns>Updated item</returns>
            public TItem Update<TItem>(TItem item)
                where TItem : class, new()
            {
                DbSet<TItem> set = GetDbSet(typeof(TItem)).GetValue(context, null) as DbSet<TItem>;
                set.Attach(item);
                context.Entry(item).State = System.Data.EntityState.Modified;
                context.SaveChanges();
                return item;
            }/// <summary>
            /// Delete an item
            /// </summary>
            /// <typeparam name="TItem">Type of item to delete</typeparam>
            /// <param name="item">Item to delete</param>
            public void Delete<TItem>(TItem item)
               where TItem : class, new()
            {
                DbSet<TItem> set = GetDbSet(typeof(TItem)).GetValue(context, null) as DbSet<TItem>;
                var entry = context.Entry(item);
                if (entry != null)
                {
                    entry.State = System.Data.EntityState.Deleted;
                }
                else
                {
                    set.Attach(item);
                }
                context.Entry(item).State = System.Data.EntityState.Deleted;
                context.SaveChanges();
            }
    
            private PropertyInfo GetDbSet(Type itemType)
            {
                var properties = typeof(TContext).GetProperties().Where(item => item.PropertyType.Equals(typeof(DbSet<>).MakeGenericType(itemType)));
    
                return properties.First();
            }
  • 相关阅读:
    2020/5/18 BUU_ [GWCTF 2019]xxor
    2020/5/17 BUU_[BJDCTF2020]BJD hamburger competition
    2020/5/17 BUU_[BJDCTF2020]easy
    2020/5/16 BUU_ [ACTF新生赛2020]easyre
    2020.5.16 15:06
    由一道逆向题而引发,IDA调试ELF文件
    【攻防世界】对于parallel-comparator-200的理解的感想
    『攻防世界』:新手区 | when_did_you_born
    『攻防世界』:新手区 | guess number
    『攻防世界』:新手区 | level3
  • 原文地址:https://www.cnblogs.com/pigwing/p/2674727.html
Copyright © 2011-2022 走看看