Beispiel #1
0
        /// <summary>
        /// Get table name of entity
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="context">Database context</param>
        /// <returns>Table name</returns>
        //public static string GetTableName<TEntity>(this UMTIDbContext context) where TEntity : BaseEntity
        //{
        //    if (context == null)
        //        throw new ArgumentNullException(nameof(context));

        //    //try to get the EF database context
        //    if (!(context is DbContext dbContext))
        //        throw new InvalidOperationException("Context does not support operation");

        //    var entityTypeFullName = typeof(TEntity).FullName;
        //    if (!tableNames.ContainsKey(entityTypeFullName))
        //    {
        //        //get entity type
        //        var entityType = dbContext.Model.FindRuntimeEntityType(typeof(TEntity));

        //        //get the name of the table to which the entity type is mapped
        //        tableNames.TryAdd(entityTypeFullName, entityType.Relational().TableName);
        //    }

        //    tableNames.TryGetValue(entityTypeFullName, out var tableName);

        //    return tableName;
        //}
        /// <summary>
        /// Get database name
        /// </summary>
        /// <param name="context">Database context</param>
        /// <returns>Database name</returns>
        public static string DbName(this UMTIDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            //try to get the EF database context
            if (!(context is DbContext dbContext))
            {
                throw new InvalidOperationException("Context does not support operation");
            }

            if (!string.IsNullOrEmpty(databaseName))
            {
                return(databaseName);
            }

            //get database connection
            var dbConnection = dbContext.Database.GetDbConnection();

            //return the database name
            databaseName = dbConnection.Database;

            return(databaseName);
        }
Beispiel #2
0
        /// <summary>
        /// Execute commands from a file with SQL script against the context database
        /// </summary>
        /// <param name="context">Database context</param>
        /// <param name="filePath">Path to the file</param>
        public static void ExecuteSqlScriptFromFile(this UMTIDbContext context, string filePath)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (!File.Exists(filePath))
            {
                return;
            }

            context.ExecuteSqlScript(File.ReadAllText(filePath));
        }
Beispiel #3
0
        /// <summary>
        /// Execute commands from the SQL script against the context database
        /// </summary>
        /// <param name="context">Database context</param>
        /// <param name="sql">SQL script</param>
        public static void ExecuteSqlScript(this UMTIDbContext context, string sql)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var sqlCommands = GetCommandsFromScript(sql);

            foreach (var command in sqlCommands)
            {
                context.ExecuteSqlCommand(command);
            }
        }
Beispiel #4
0
 public UMTRepository(UMTIDbContext dbContext) : base(dbContext)
 {
 }
Beispiel #5
0
 public UMTProvider(UMTIDbContext dbContext)
 {
     repository = new UMTRepository(dbContext);
 }
Beispiel #6
0
 public UMTEfRepository(UMTIDbContext context)
 {
     this._context = context;
 }
 public WeatherForecastController(ILogger <WeatherForecastController> logger, IUMTProvider UMTProvider, UMTIDbContext udbContext)
 {
     _logger           = logger;
     this._umtProvider = UMTProvider;
     this._udbContext  = udbContext;
 }
Beispiel #8
0
        public static FunctionInfo GetCommandFromMethod(this UMTIDbContext context, MethodInfo methed, params object[] parameterValues)
        {
            FunctionInfo function = new FunctionInfo();

            try
            {
                var Function = methed.GetCustomAttributes(typeof(FunctionAttribute));
                if (Function != null && Function.Any())
                {
                    var func = (FunctionAttribute)Function.FirstOrDefault();
                    function.Query = func.Name;
                    var Parameters = methed.GetParameters();
                    if (Parameters != null && Parameters.Any())
                    {
                        function.Parameters = new System.Data.Common.DbParameter[Parameters.Count()];
                        int i = 0;
                        foreach (var item in Parameters)
                        {
                            var param         = item.GetCustomAttribute <ParameterAttribute>();
                            var p             = new EfDataProviderManager().DataProvider.GetParameter();
                            var ParameterName = "";
                            if (param != null)
                            {
                                ParameterName = param.Name;
                            }
                            else
                            {
                                ParameterName = item.Name;
                            }
                            p.ParameterName = ParameterName;
                            p.Value         = parameterValues[item.Position];
                            if (item.ParameterType.IsByRef)
                            {
                                p.Direction = ParameterDirection.InputOutput;
                            }
                            else if (item.IsOut)
                            {
                                p.Direction = ParameterDirection.Output;
                            }
                            else
                            {
                                p.Direction = ParameterDirection.Input;
                            }
                            function.Parameters[i] = p;
                            i++;
                        }
                    }

                    /*
                     * var returnParameter = cmd.CreateParameter();
                     * returnParameter.ParameterName = "@return_value";
                     * returnParameter.DbType = DbType.Int32;
                     * returnParameter.Direction = ParameterDirection.ReturnValue;
                     * cmd.Parameters.Add(returnParameter);
                     */
                    return(function);
                }
                throw new ArgumentNullException("Function", "Function name is not entry.");
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Beispiel #9
0
 public static UMTDataContext As(this UMTIDbContext context)
 {
     return((UMTDataContext)context);
 }