How to: Remove a table from the database
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This article will demonstrate to you how you can drop a given table that became obsolete from your database. Let's take a look at an example that uses SofiaCarRental and requires the RentalRates table to be removed.
-
Find the MetadataSource file of your fluent model in Solution Explorer and open it
-
In the PrepareMapping() method, locate the mapping configuration for the RentalRate class and mark it with the Drop() method before adding it in the list with the mapping configurations of the model
In order to use the Drop() method you need to add a using / Imports clause to the Telerik.OpenAccess.Metadata.Fluent.Advanced namespace in your code.
protected override IList<MappingConfiguration> PrepareMapping() { List<MappingConfiguration> mappingConfigurations = new List<MappingConfiguration>(); MappingConfiguration<RentalRate> rentalrateConfiguration = this.GetRentalRateMappingConfiguration(); rentalrateConfiguration.Drop(); mappingConfigurations.Add(rentalrateConfiguration); //Mapping configurations for other types return mappingConfigurations; } public MappingConfiguration<RentalRate> GetRentalRateMappingConfiguration() { MappingConfiguration<RentalRate> configuration = this.GetRentalRateClassConfiguration(); this.PrepareRentalRatePropertyConfigurations(configuration); this.PrepareRentalRateAssociationConfigurations(configuration); return configuration; } public MappingConfiguration<RentalRate> GetRentalRateClassConfiguration() { MappingConfiguration<RentalRate> configuration = new MappingConfiguration<RentalRate>(); configuration.MapType(x => new { }). WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed). ToTable("RentalRates"); return configuration; } public void PrepareRentalRatePropertyConfigurations(MappingConfiguration<RentalRate> configuration) { configuration.HasProperty(x => x.RentalRateID).IsIdentity(KeyGenerator.Autoinc). HasFieldName("_rentalRateID").WithDataAccessKind(DataAccessKind.ReadWrite). ToColumn("RentalRateID").IsNotNullable().HasColumnType("int"). HasPrecision(0).HasScale(0); configuration.HasProperty(x => x.CategoryID).HasFieldName("_categoryID"). WithDataAccessKind(DataAccessKind.ReadWrite). ToColumn("CategoryID").IsNullable().HasColumnType("int"). HasPrecision(0).HasScale(0); configuration.HasProperty(x => x.Daily).HasFieldName("_daily"). WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Daily"). IsNullable().HasColumnType("smallmoney").HasPrecision(0).HasScale(0); configuration.HasProperty(x => x.Weekly).HasFieldName("_weekly"). WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Weekly"). IsNullable().HasColumnType("smallmoney").HasPrecision(0).HasScale(0); configuration.HasProperty(x => x.Monthly).HasFieldName("_monthly"). WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Monthly"). IsNullable().HasColumnType("smallmoney").HasPrecision(0).HasScale(0); } public void PrepareRentalRateAssociationConfigurations(MappingConfiguration<RentalRate> configuration) { configuration.HasAssociation(x => x.Category).HasFieldName("_category"). WithOpposite(x => x.RentalRates).ToColumn("CategoryID"). HasConstraint((x, y) => x.CategoryID == y.CategoryID). WithDataAccessKind(DataAccessKind.ReadWrite); }
Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration) Dim mappingConfigurations As New List(Of MappingConfiguration)() Dim rentalrateConfiguration As MappingConfiguration(Of RentalRate) =_ Me.GetRentalRateMappingConfiguration() rentalrateConfiguration.Drop() mappingConfigurations.Add(rentalrateConfiguration) 'Mapping configurations for other types Return mappingConfigurations End Function Public Function GetRentalRateMappingConfiguration() As MappingConfiguration(Of RentalRate) Dim configuration As MappingConfiguration(Of RentalRate) =_ Me.GetRentalRateClassConfiguration() Me.PrepareRentalRatePropertyConfigurations(configuration) Me.PrepareRentalRateAssociationConfigurations(configuration) Return configuration End Function Public Function GetRentalRateClassConfiguration() As MappingConfiguration(Of RentalRate) Dim configuration As New MappingConfiguration(Of RentalRate)() configuration.MapType(Function(x) New With {x})._ WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed)._ ToTable("RentalRates") Return configuration End Function Public Sub PrepareRentalRatePropertyConfigurations(configuration As MappingConfiguration(Of RentalRate)) configuration.HasProperty(Function(x) x.RentalRateID).IsIdentity(KeyGenerator.Autoinc)._ HasFieldName("_rentalRateID").WithDataAccessKind(DataAccessKind.ReadWrite)._ ToColumn("RentalRateID").IsNotNullable().HasColumnType("int")._ HasPrecision(0).HasScale(0) configuration.HasProperty(Function(x) x.CategoryID).HasFieldName("_categoryID")._ WithDataAccessKind(DataAccessKind.ReadWrite)._ ToColumn("CategoryID").IsNullable().HasColumnType("int")._ HasPrecision(0).HasScale(0) configuration.HasProperty(Function(x) x.Daily).HasFieldName("_daily")._ WithDataAccessKind(DataAccessKind.ReadWrite)._ ToColumn("Daily").IsNullable().HasColumnType("smallmoney")._ HasPrecision(0).HasScale(0) configuration.HasProperty(Function(x) x.Weekly).HasFieldName("_weekly")._ WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Weekly")._ IsNullable().HasColumnType("smallmoney")._ HasPrecision(0).HasScale(0) configuration.HasProperty(Function(x) x.Monthly).HasFieldName("_monthly")._ WithDataAccessKind(DataAccessKind.ReadWrite)._ ToColumn("Monthly").IsNullable().HasColumnType("smallmoney")._ HasPrecision(0).HasScale(0) End Sub Public Sub PrepareRentalRateAssociationConfigurations(configuration As MappingConfiguration(Of RentalRate)) configuration.HasAssociation(Function(x) x.Category).HasFieldName("_category")._ WithOpposite(Function(x) x.RentalRates).ToColumn("CategoryID")._ HasConstraint(Function(x, y) x.CategoryID = y.CategoryID )._ WithDataAccessKind(DataAccessKind.ReadWrite) End Sub
Create/Migrate your database with the UpdateSchema() method
The script that will be generated with the properties of the SchemaUpdateProperties class set to false in the CreateUpdateDDLScript() method of UpdateSchema() would look like this one:
-- dropping table [RentalRates]
DROP TABLE [RentalRates]
go
Dropped Database Objects According to The Relationships of The Marked Table
Relationship | Table to Drop | Outcome |
One-To-Many | Parent or Child | The marked table and the association are dropped |
One-To-One | Parent or Child | The marked table and the association are dropped |
Many-To-Many | Parent or Child | The marked table, the join table and the constraints are dropped |