How to recreate my EF CodeFirst Table?
Asked Answered
T

4

10

I created a Code First class

A database was created in Sql automatically.

I deleted the tables :(

I have migrations enabled:

AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
Database.SetInitializer(new DropCreateDatabaseAlways<SurveyContext>());

Yet upon

update-database -force

Cannot find the object "dbo.CustomerResponses" because it does not exist or you do not have permissions.

Please help.

Theolatheologian answered 30/1, 2013 at 16:51 Comment(2)
What does your context look like?Melentha
have you tried to google the "Cannot find the object because it does not exist or you do not have permissions"?Chill
T
9

In system tables there is a table called

__MigrationHistory

...I deleted it.

In Package manager console ran command "update-database" Problem fixed!

More info:

http://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system-table/

Theolatheologian answered 30/1, 2013 at 16:51 Comment(4)
Seems to work only if you have migration information defined in your project No migrations configuration type was found in the assembly '{project_name}'.. But it was worth a try.Lambda
handy link to the topic at hand - msdn.microsoft.com/en-us/data/jj591621.aspxLambda
The downside of this solution is that you lose your migration history.Bayberry
i tried this but then it wouldn't update because the other tables already existWyandotte
B
6

Daf De Giraf has a better answer here that doesn't wipe out your migration history.

If you worked the correct way to create your migrations by using the command Add-Migration "Name_Of_Migration" then you can do the following to get a clean start (reset, with loss of data, of course):

  1. Update-database -TargetMigration:0
    Normally your DB is empty now since the down methods were executed.

  2. Update-database This will recreate your DB to your current migration

Bayberry answered 22/6, 2015 at 22:12 Comment(0)
E
0

I had that error message in the past....

Check if the database that you've specified in the connection string is created. If it's not, create it and then execute the code.

Extravaganza answered 30/1, 2013 at 19:38 Comment(0)
S
0

Obviously there may be a better way of doing this, but as this was surprisingly top Google account I thought it would be good to mention the idea i had after reading these answers:

Here's the issue i faced: I was changing FK's generated by EF6 code first and came across the error:

Unable to determine the relationship represented by navigation property 'Account.SalaryIncomes' of type 'ICollection<Person>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I attempted many ways of defining the Mapping in the hopes of resolving this error, however i ended up dropping my Person table, and after removing the the relationship entirely in an attempt to start from scratch, the error was resolved. However when updating the database, it complained that "Person" did not exist - which lead me here.

After reviewing these answers, I decided I was too lazy to reload the data and thought that the migrations generated C# UP() and Down() methods - I thought, maybe i can reference another migration... after extracting the up and down code into public methods in that migration, and referencing those from the new migration - the update worked and person was then created. Just to be safe - i wrapped the new calls in try catch... do my new UP method looks like this:

protected override void Up(MigrationBuilder migrationBuilder)
    {
        try
        {
            new AddPeople().CreatePersonTable(migrationBuilder);
        }
        catch { }

        // Origional generated migration code
    }

This answer comes with the caviat that I have not tested to see if this migration will be overridden next time (loosing the code) - however if the migrations are run in order, then pressingly you can in fact run this once, and then remove it after.

Senhor answered 23/9, 2018 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.