Efcore: Does ef core provide some api to retrive information from schema ? indexes,FK, dependency on column

Created on 27 Aug 2018  路  2Comments  路  Source: dotnet/efcore

I'a realizing custom mechanism of migrations

I have question.

I want delete column by this code . But Ms sql tel me that for this Foreign key have
"The index 'IX_entity22_ref2id' is dependent on column 'ref2id'.
鈫礣he object 'FK_entity22_Entity1_ref2id' is dependent on column 'ref2id'.
鈫礎LTER TABLE DROP COLUMN ref2id failed because one or more objects access this column."

Does ef core provide some api to retrive information from schema ?
For example i want delete column, i retrive information from schema and delete indexes and FK, atfer all delete column ?

 public static int DeleteField(DevDBcontext db, string entityname, EntityFieldsDef field)
        {
            int result = 0;
            MigrationBuilder migrationBuilder = new MigrationBuilder(ActiveProvider);
            var fieldname = "";
            if (field.isreference)
            {
                fieldname = field.name + "id";
            }
            else {
                fieldname = field.name;
            }

            migrationBuilder.DropColumn(name: fieldname, table: entityname);
            var sqlGenerator = db.GetService<IMigrationsSqlGenerator>();
            var commands = sqlGenerator.Generate(migrationBuilder.Operations, null);
            var executor = db.GetService<IMigrationCommandExecutor>();
            executor.ExecuteNonQuery(commands, db.GetService<IRelationalConnection>());
            result = 1;
            return result;
        }

```
Exception message:
Stack trace:

### Steps to reproduce
```c#

Further technical details

Microsoft.EntityFrameworkCore.Tools 2.0.2
IDE: (e.g. Visual Studio 2017 15.4)

closed-question customer-reported

Most helpful comment

DatabaseModel has this information. Get one like this:

var serviceCollection = new ServiceCollection()
    .AddEntityFrameworkDesignTimeServices();
new SqlServerDesignTimeServices().ConfigureDesignTimeServices(serviceCollection);
var services = serviceCollection.BuildServiceProvider();
var databaseModelFactory = services.GetRequiredService<IDatabaseModelFactory>();

var databaseModel = databaseModelFactory.Create(
    connectionString,
    tables: new string[0], // All tables
    schemas: new string[0]); // All schemas

(This is part of the Reverse Engineering logic that backs the dotnet ef dbcontext scaffod command.)

All 2 comments

DatabaseModel has this information. Get one like this:

var serviceCollection = new ServiceCollection()
    .AddEntityFrameworkDesignTimeServices();
new SqlServerDesignTimeServices().ConfigureDesignTimeServices(serviceCollection);
var services = serviceCollection.BuildServiceProvider();
var databaseModelFactory = services.GetRequiredService<IDatabaseModelFactory>();

var databaseModel = databaseModelFactory.Create(
    connectionString,
    tables: new string[0], // All tables
    schemas: new string[0]); // All schemas

(This is part of the Reverse Engineering logic that backs the dotnet ef dbcontext scaffod command.)

Looking deeper in ef realising that ef has got great potencial! Thanks fellows!

Was this page helpful?
0 / 5 - 0 ratings