❔ How to migrate a virtual ICollection to a table on my SQL database?
I have my model and it includes ICollections of different models that I would also like to save. I have never used an SQL database before I do not know what data type and structure to use for the virtual ICollections.
12 Replies
I think it's a good idea if you create a basic proof of concept using EntityFramework before you consider migrating
In the last picture you seem to create the query yourself but EntityFramework can also do this for you, using migrations
the
virtual
keyword is only required if you have lazy loading
In your case Orientation
and PowerResults
just need a reference back to the project in order to complete the one-to-many relation. If you need a many-to-many, you will need to do some more though.
Just remember that EntityFramework is smart enough to create the relation using migrations, if you use that
So you don't really need to do all the queries yourself.In this case I believe it is one to many as just one project will relate to multiple orientations and power results. I have never done this before nor used entity framework so bear with me here, how do I use it to create these migrations for me?
Do you have a DbContext? This basically defines your context and it will create your queries.
work in progress but yes
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-7.0 Maybe this link helps
Tutorial: Get started with EF Core in an ASP.NET MVC web app
This page is the first in a series of tutorials that explain how to build the Contoso University sample EF/MVC app
This explains pretty well how to work with EntityFramework, how relations work
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations?source=recommendations&view=aspnetcore-7.0 This link kind of explains how migrations work. Maybe you can apply it to your database
Tutorial Part 5, apply migrations to the Contoso University sample
Part 5 of the Contoso University tutorial series. Use the EF Core migrations feature for managing data model changes in an ASP.NET Core MVC app.
EntityFramework is huge and complex. If this migration is for an important project, I would advice you know pretty well how it works, as it works with a lot of complex subjects such as having the ability to track changes, work with a fluent api, many attributes, etcetera
It's all really well done though
Okay thank you, I'll see what I can do! And I appreciate it, this is my first and only c# project I've worked on and the rest of it I've managed to figure out but I haven't been able to wrap my head around this part yet so thank you.
No problem, in my opinion they made it really easy to jump into as long as you put your head into it, so I'm sure you'll get used to it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.