Entity Framework Code First (2021, DotNet Core or DotNet 5)
HTML-код
- Опубликовано: 5 июл 2021
- Entity Framework Code First from scratch - build a new project and create data classes, then generate the database from your models using Entity Framework (Code First). This video also shows you then how to query the database using EF after you have generated the schema, and up to apply migrations to update/scaffold the database. Examples are in SQL server but could be applied to other similar databases like MySQL.
Наука
Great job! I was looking for a great guide on this topic and here it is. 🦍
As someone coming from Dapper, this saves so much time and works like magic!
Yes! Entity framework is so much of a time saver.
Amazing tutorial! Thanks!
Glad it was helpful!
thank you very much, it was excellent explanation for my problems.
Emrah, Glad the explanation helped you!
Excelent, your english is very clear
Thank you!
Excellent... Thank you man thank you
You are welcome!
This was helpful, thank you
You're welcome!
Thank you! i have a question, i moved from visual studio 2019 to 2021, but now how do I use the package manager, it seems I need to reset the migration and restart again?
@hq cart, the package manager in visual studio 2022 is for console: Tools->Nuget Package Manager-> Package Manager Console or for UI: Right click on solution or project -> Manage Nuget Packages
This is pretty much the same as visual studio 2019. Yours might just not be open by default, so once you open it the first time it may resemble closer to your previous environment. Best of luck!
Good presentation. Everything was clear. Here are my questions. 1. How do you use the data annotation to display error messages in a production environment especially the required attribute. 2. Is dependency injection supported in console or window application when using entity framework core in these environments.
For the first question, I think this is what you are after: docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations
For the second question, the answer is yes. You can setup entity framework basically the same way, do codefirst or database first. Then create your dependency injection and move the query logic out into a service. This link probably doesn't directly answer your question but might be helpful since I guess the more involved portion here is really just moving the EF references out to a service, then injecting that service into your classes: docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-usage
It's insane how much extra work you have to do for codefirst development. I'm baffled that anyone thinks this is a good idea.
I guess a lot of us get interested in the idea so we can rebuild a database or run a migration based ona change for a client but your right, in the end it seems to be more work. I'm doing more database first now myself.
@@DataVids thanks for the honest reply. I see where codefirst works in some ways but its just so much easier to make db changes in the visual editor and just right click on the edmx surface and do an 'update'. Everything works instantly.
Amazing Thank you for sharing
Thanks for watching!
thankyou very much ! the explanation is so simple that i can easily understand the concept !
Leo Aldi, You are welcome!
Thank you sir
Hello, have you heard about any changes for Code First approach in .NET 6?
@Hazuuur it does not seem like there is a whole lot, but some minor functionality added. See here this link: docs.microsoft.com/en-us/ef/ef6/what-is-new/
Muy bueno 👍
One more, in the onconfiguring override method, is it possible to pass the name of the connection string from app.config file
If its a webproject, then its probably appsettings.json, and you can get it like this: options.UseSqlServer(Configuration.GetConnectionString("myConnectionStringName"))); If its a winforms or wpf project then you can get it from the App.config file like this: optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["myConnectionStringName"].ConnectionString);
Thanks man!
You bet!
Hello. I am using Mac with M1 chip. I tried command ' dotnet ef database update ' but I got this error:
System.PlatformNotSupportedException: LocalDB is not supported on this platform.
Do you know what is the problem?
Thank you so much!
I haven't bumped into this problem before but reading your error message, it sounds like possibly your connection string needs to be fixed or if it really is the platform then you might try to run this in a VM (virtual machine using software like virtual box or vmware) to change the platform. My personal preference has never been to work on Mac's, but some of my coworkers use Mac's and they have virtual machines that they use for certain programs.
how can we make connections of it with flutter #dart using Sql Server please guide or a video suggestion?
Sorry, I have not yet learned Flutter/Dart, though it does sound like a good tech stack.
sir how to create login page for pos system using sqlite and c#
It sounds like you are looking for a whole application! I think you should check out my identity videos though, these are making a login system using c# with SQL server. Doing such with SQLlight should be a minor change, but you might want to persist some of the login information outside of memory too.
thanks!
Do you have a database first approach video?
I have a few:
Entity framework database first:
ruclips.net/video/ByWkSHBwnyo/видео.html
MySQL database first project:
ruclips.net/video/iKSj3AyGq8w/видео.html
Add identity to existing database first project:
ruclips.net/video/5X9ogJVWAjE/видео.html
This keeps happening where the comments show up in my email but don't make it to the channel. Perhaps your comments are being flagged? Responding to @Mitesh M If you have a local connection from your pc to sql express, then you can setup either sql server authentication or you can setup windows active directory authentication. If you use the default ad connection we can use 'trusted connection', and so your connectionstring may be similiar to this: "Server=DESKTOP-yourPCName\\SQLEXPRESS;Database=Northwind; Trusted_Connection=True;" and don't use the double backslash (just one backslash) if you are referring to the EF console command to generate scaffolding or models, etc. use the double if its in appsettings.json. Hope that helps you and/or someone else and hopefully your comments start showing up!
Hi @16:45 you use for DataAnnotation MaxLength, i use StringLength whats the diffrence
It looks like the recommended choice by Microsoft/MSDN for user validation is StringLength and the recommended for building a database from code first is MaxLength.
@@DataVids So my Model has MaxLength but my ViewModel will have StringLength, right? I have now in both StringLength and its working totally fine
Source code is here at this github link, but I advise watching the video first as it will explain the where/why and how to generate the generated portions..:
github.com/DataVids/EntityFramework/tree/main/EFCodeFirst
Yeah!!! It's been along time, shouldn't have left you, without a Data Vid to step to. 🤣🤣🤣1OVE.
Now youz CANT leave. You broke ma heart - no other data vid is like me! haha Life is crazy, good to see you still posting and funny. Cheers
where can i check my connection string in ssms
You can look at server explorer or you can build a query to get what you want. stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database
@@DataVids thanx i got it .amazing video btw
Wait a minute. Before you scaffolded and performed a database update, the database itself already exist, you only added the tables to it. Right?
This could go either way, but in most cases code-first means that there are no tables and you are designing the tables in C# then letting dotnet build the tables in your database. If the connection string you use is to a database that has some existing tables, that should be OK. If you want to have all the tables designed in SQL first then have models created from it, then you should instead do 'database first' instead of 'code first'. Hope that helps.
@@DataVids Yes, but in your code, that database did exist already, right? Then you can add tables later to it, that I get.
thank you
You're welcome!
красава, чувак
Большое спасибо! (Google translate !)
Thanks for your video, but Entity Framework is totally removed from our system, it's just too messy and difficult to maintain for all developers. It could work in a very small system but not the enterprise level! Not sure what Microsoft was thinking when they implemented this!
Thanks for watching DataVids Jack! Sorry you had such a bad experience, but I have worked with multiple large products and not experienced such issues - though I do think code-first is not a good fit for enterprise, you might want to look into database-first entity framework. Its nice to see in a large environment that when you regenerate the models for example, that one developers changes are going to break someone elses queries and this is a nice benefit in large dev environments when using EF.
i am a dog
Interesting!