Entity Framework not creating tables
I am using code first workflow. Entity framework was creating tables for my models automatically upon adding migrations. It has stopped doing so. How can I fix this issue?
15 Replies
Are those tables added to the dbcontext?
Let me check real quick
I haven't needed to manually add them into dbcontext since my first couple tables
it was reading my model changes automatically
What I mean, is
DbSet<TheModel>
properties
If not, are they referenced in other models?
If you have Foo
and it references Bar
, you only need a DbSet<Foo>
and EF will figure out that Bar
should also be a table
Not the best ideal, it's better to add everything as a dbset, but it does work this wayAh ok I understand. I forgot where dbcontext was because I have been referencing other models in models that are in dbcontext
😦
found it, ok I will add it to dbset
thank you
@Angius I have another issue now. When trying to reference a model in another model i am getting this error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Movies_dbo.Genres_GenreId". The conflict occurred in database "aspnet-web_template_001-20230102051540", table "dbo.Genres", column 'Id'.
Show code
And the genre?
the migration worked and created the proper table
but now when using Genre inside Movie as a property I get this error
The
Id
in Genre
doesn't have the [Key]
attributeOk I will add it but it was working on other classes without adding the attribute. I will see if it fixes.
How can I rollback migrations to a previous migration and try to fix from there. I am still getting the same error
dotnet ef migrations remove
can i rollback to a specific migration
Yeah
EF Core tools reference (.NET CLI) - EF Core
Reference guide for the Entity Framework Core .NET Core CLI tools
perfect i will check it out and msg here if i have problems
thanks so much