Пример #1
0
        internal MetaTable(MetaModel model, TableProvider provider, ContextConfiguration configuration)
        {
            bool scaffoldAllTables;

            this.model = model;
            Provider   = provider;
            if (configuration != null)
            {
                ScaffoldAllTables = scaffoldAllTables = configuration.ScaffoldAllTables;
                Func <Type, TypeDescriptionProvider> factory = configuration.MetadataProviderFactory;
                if (factory != null)
                {
                    Type t = EntityType;
                    TypeDescriptionProvider p = factory(t);
                    if (p != null)
                    {
                        TypeDescriptor.AddProvider(p, t);
                    }
                }
            }
            else
            {
                scaffoldAllTables = false;
            }

            ScaffoldTableAttribute attr = null;

            MetaModel.GetDataFieldAttribute <ScaffoldTableAttribute> (Attributes, ref attr);
            Scaffold        = attr != null ? attr.Scaffold : scaffoldAllTables;
            DataContextType = provider.DataModel.ContextType;

            var        columns               = new List <MetaColumn> ();
            var        primaryKeyColumns     = new List <MetaColumn> ();
            var        foreignKeyColumnNames = new List <string> ();
            MetaColumn mc;

            foreach (var c in provider.Columns)
            {
                // this seems to be the determining factor on whether we create
                // MetaColumn or MetaForeignKeyColumn/MetaChildrenColumn. As the
                // determination depends upon the relationship direction, we must
                // check that using the ColumnProvider's association, if any.
                //
                //  http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metaforeignkeycolumn.aspx
                //  http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metachildrencolumn.aspx
                //  http://forums.asp.net/t/1426992.aspx
                var association = c.Association;
                if (association == null)
                {
                    mc = new MetaColumn(this, c);
                }
                else
                {
                    var dir = association.Direction;
                    if (dir == AssociationDirection.OneToOne || dir == AssociationDirection.ManyToOne)
                    {
                        mc = new MetaForeignKeyColumn(this, c);
                    }
                    else
                    {
                        mc = new MetaChildrenColumn(this, c);
                    }
                }

                columns.Add(mc);
                if (c.IsPrimaryKey)
                {
                    primaryKeyColumns.Add(mc);
                }

                if (mc is MetaForeignKeyColumn)
                {
                    foreignKeyColumnNames.Add(c.Name);
                }
            }

            Columns           = new ReadOnlyCollection <MetaColumn> (columns);
            PrimaryKeyColumns = new ReadOnlyCollection <MetaColumn> (primaryKeyColumns);
            if (foreignKeyColumnNames.Count == 0)
            {
                ForeignKeyColumnsNames = String.Empty;
            }
            else
            {
                ForeignKeyColumnsNames = String.Join(",", foreignKeyColumnNames.ToArray());
            }

            HasPrimaryKey = primaryKeyColumns.Count > 0;

            // See http://forums.asp.net/t/1388561.aspx
            //
            // Also, http://forums.asp.net/t/1307243.aspx - that seems to be out of
            // scope for us, though (at least for now)
            IsReadOnly = primaryKeyColumns.Count == 0;

            // FIXME: fill more properties.
        }
Пример #2
0
		internal MetaTable (MetaModel model, TableProvider provider, ContextConfiguration configuration)
		{
			bool scaffoldAllTables;
			
			this.model = model;
			Provider = provider;
			if (configuration != null) {
				ScaffoldAllTables = scaffoldAllTables = configuration.ScaffoldAllTables;
				Func <Type, TypeDescriptionProvider> factory = configuration.MetadataProviderFactory;
				if (factory != null) {
					Type t = EntityType;
					TypeDescriptionProvider p = factory (t);
					if (p != null)
						TypeDescriptor.AddProvider (p, t);
				}
			} else
				scaffoldAllTables = false;
			
			ScaffoldTableAttribute attr = null;
			MetaModel.GetDataFieldAttribute <ScaffoldTableAttribute> (Attributes, ref attr);
			Scaffold = attr != null ? attr.Scaffold : scaffoldAllTables;
			DataContextType = provider.DataModel.ContextType;
			
			var columns = new List <MetaColumn> ();
			var primaryKeyColumns = new List <MetaColumn> ();
			var foreignKeyColumnNames = new List <string> ();
			MetaColumn mc;
			
			foreach (var c in provider.Columns) {
				// this seems to be the determining factor on whether we create
				// MetaColumn or MetaForeignKeyColumn/MetaChildrenColumn. As the
				// determination depends upon the relationship direction, we must
				// check that using the ColumnProvider's association, if any.
				//
				//  http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metaforeignkeycolumn.aspx
				//  http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metachildrencolumn.aspx
				//  http://forums.asp.net/t/1426992.aspx
				var association = c.Association;
				if (association == null)
					mc = new MetaColumn (this, c);
				else {
					var dir = association.Direction;
					if (dir == AssociationDirection.OneToOne || dir == AssociationDirection.ManyToOne)
						mc = new MetaForeignKeyColumn (this, c);
					else
						mc = new MetaChildrenColumn (this, c);
				}
				
				columns.Add (mc);
				if (c.IsPrimaryKey)
					primaryKeyColumns.Add (mc);

				if (mc is MetaForeignKeyColumn)
					foreignKeyColumnNames.Add (c.Name);
			}
			
			Columns = new ReadOnlyCollection <MetaColumn> (columns);
			PrimaryKeyColumns = new ReadOnlyCollection <MetaColumn> (primaryKeyColumns);
			if (foreignKeyColumnNames.Count == 0)
				ForeignKeyColumnsNames = String.Empty;
			else
				ForeignKeyColumnsNames = String.Join (",", foreignKeyColumnNames.ToArray ());
			
			HasPrimaryKey = primaryKeyColumns.Count > 0;

			// See http://forums.asp.net/t/1388561.aspx
			//
			// Also, http://forums.asp.net/t/1307243.aspx - that seems to be out of
			// scope for us, though (at least for now)
			IsReadOnly = primaryKeyColumns.Count == 0;
			
			// FIXME: fill more properties.
		}