0

I've seen posts about MetadataWorkspace but no examples for this task. I want my code to detect if the database has had a new table added so I'm comparing the number of tables in the DB with 2 lists of known tables. A table can be either user data or system data and I want to delete all the data in the user tables.

If a new table is discovered, the code will ask the user to define whether it contains system or user data.

1 Answer 1

3

This should help you. Here is the relevant part of it for you:

var metadata = ((IObjectContextAdapter)db).ObjectContext.MetadataWorkspace;

    var tables = metadata.GetItemCollection(DataSpace.SSpace)
      .GetItems<EntityContainer>()
      .Single()
      .BaseEntitySets
      .OfType<EntitySet>()
      .Where(s => !s.MetadataProperties.Contains("Type") 
        || s.MetadataProperties["Type"].ToString() == "Tables");
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Interestingly, it did not return __MigrationHistory but that's ok.
@BrianLeeming I believe that is because that part of the code queries against the EF model. Take a look at the section of that article called "What about the database?". That part might get you the migrationhistory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.