Esempio n. 1
0
        private void InitializeDbSets()
        {
            var dbSetType        = typeof(DbSet <>);
            var dbSetDescriptors = TypeDescriptor.GetProperties(this).Cast <PropertyDescriptor>()
                                   .Where(p => p.PropertyType.IsConstructedGenericType && p.PropertyType.GetGenericTypeDefinition().Equals(dbSetType));
            var entityTypes = dbSetDescriptors.Select(p => p.PropertyType.GenericTypeArguments[0]);

            foreach (var descriptor in dbSetDescriptors)
            {
                Type   entityType      = descriptor.PropertyType.GenericTypeArguments[0];
                Type   entityDbSetType = dbSetType.MakeGenericType(entityType);
                var    options         = new DbSetOptions(this.connection, entityType, primaryKey);
                object dbSet           = Activator.CreateInstance(entityDbSetType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { options, this.dataSet }, CultureInfo.InvariantCulture);
                descriptor.SetValue(this, dbSet);
            }
        }
Esempio n. 2
0
        internal DbSet(DbSetOptions options, DataSet dataSet)
        {
            this.options = options ?? throw new ArgumentNullException(nameof(options));
            this.adapter = new SqlDataAdapter();
            this.dataSet = dataSet;
            string tableName = this.options.Table;

            this.adapter.SelectCommand = new SqlCommand($"SELECT * FROM {tableName} WHERE 1=2", this.options.SqlConnection);
            this.dataTable             = new DataTable();
            this.adapter.Fill(dataTable);
            this.dataTable.TableName = tableName;
            this.dataSet.Tables.Add(this.dataTable);
            var sqlCommandBuilder = new SqlCommandBuilder(this.adapter);

            this.outIdParameter           = new SqlParameter("@id", SqlDbType.Int);
            this.outIdParameter.Direction = ParameterDirection.Output;

            Provider   = new SqlProvider <TEntity>(this);
            Expression = Expression.Constant(this);

            this.mapper         = new RowMapper(this.options);
            this.commandBuilder = new CommandBuilder(options);
        }