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.
  • НаукаНаука

Комментарии • 65

  • @dizmo..
    @dizmo.. Год назад +1

    Great job! I was looking for a great guide on this topic and here it is. 🦍

  • @DanFlakes
    @DanFlakes 2 года назад +2

    As someone coming from Dapper, this saves so much time and works like magic!

    • @DataVids
      @DataVids  2 года назад +1

      Yes! Entity framework is so much of a time saver.

  • @rolexpbargayo
    @rolexpbargayo 2 года назад +2

    Amazing tutorial! Thanks!

    • @DataVids
      @DataVids  2 года назад

      Glad it was helpful!

  • @emrahkzltan9798
    @emrahkzltan9798 2 года назад +1

    thank you very much, it was excellent explanation for my problems.

    • @DataVids
      @DataVids  2 года назад +1

      Emrah, Glad the explanation helped you!

  • @agustinavilacaldera6968
    @agustinavilacaldera6968 2 года назад +1

    Excelent, your english is very clear

  • @farshaadmusic8351
    @farshaadmusic8351 2 года назад +1

    Excellent... Thank you man thank you

    • @DataVids
      @DataVids  2 года назад +1

      You are welcome!

  • @kaushikchauhan8557
    @kaushikchauhan8557 2 года назад +1

    This was helpful, thank you

  • @hqcart1
    @hqcart1 2 года назад +1

    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?

    • @DataVids
      @DataVids  2 года назад

      @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!

  • @taiwobabalola4188
    @taiwobabalola4188 2 года назад +1

    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.

    • @DataVids
      @DataVids  2 года назад

      For the first question, I think this is what you are after: docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations

    • @DataVids
      @DataVids  2 года назад

      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

  • @allenbythesea
    @allenbythesea 2 года назад +2

    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.

    • @DataVids
      @DataVids  2 года назад +1

      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.

    • @allenbythesea
      @allenbythesea 2 года назад

      @@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.

  • @akshayinuk
    @akshayinuk 2 года назад +1

    Amazing Thank you for sharing

    • @DataVids
      @DataVids  2 года назад

      Thanks for watching!

  • @leoaldi9379
    @leoaldi9379 2 года назад +1

    thankyou very much ! the explanation is so simple that i can easily understand the concept !

    • @DataVids
      @DataVids  2 года назад

      Leo Aldi, You are welcome!

  • @huseyincumal5259
    @huseyincumal5259 2 года назад +1

    Thank you sir

  • @Hazuuur
    @Hazuuur 2 года назад +1

    Hello, have you heard about any changes for Code First approach in .NET 6?

    • @DataVids
      @DataVids  2 года назад +1

      @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/

  • @hector9079
    @hector9079 3 года назад

    Muy bueno 👍

  • @taiwobabalola4188
    @taiwobabalola4188 2 года назад +1

    One more, in the onconfiguring override method, is it possible to pass the name of the connection string from app.config file

    • @DataVids
      @DataVids  2 года назад

      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);

  • @walmartbag777
    @walmartbag777 2 года назад +1

    Thanks man!

  • @VukotaDjLewu95
    @VukotaDjLewu95 Год назад +1

    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!

    • @DataVids
      @DataVids  Год назад

      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.

  • @awaishaider4263
    @awaishaider4263 2 года назад +1

    how can we make connections of it with flutter #dart using Sql Server please guide or a video suggestion?

    • @DataVids
      @DataVids  Год назад

      Sorry, I have not yet learned Flutter/Dart, though it does sound like a good tech stack.

  • @sivasothytharsi1203
    @sivasothytharsi1203 Год назад +1

    sir how to create login page for pos system using sqlite and c#

    • @DataVids
      @DataVids  Год назад +1

      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.

  • @Catuchee
    @Catuchee 2 года назад +1

    thanks!

  • @troymitchel4790
    @troymitchel4790 2 года назад +2

    Do you have a database first approach video?

    • @DataVids
      @DataVids  2 года назад

      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

  • @DataVids
    @DataVids  3 года назад

    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!

  • @m3xpl4y
    @m3xpl4y 2 года назад +1

    Hi @16:45 you use for DataAnnotation MaxLength, i use StringLength whats the diffrence

    • @DataVids
      @DataVids  2 года назад +1

      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.

    • @m3xpl4y
      @m3xpl4y 2 года назад

      @@DataVids So my Model has MaxLength but my ViewModel will have StringLength, right? I have now in both StringLength and its working totally fine

  • @DataVids
    @DataVids  3 года назад +2

    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

  • @PIMARINE
    @PIMARINE 3 года назад +2

    Yeah!!! It's been along time, shouldn't have left you, without a Data Vid to step to. 🤣🤣🤣1OVE.

    • @DataVids
      @DataVids  3 года назад

      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

  • @namansharma1448
    @namansharma1448 2 года назад +1

    where can i check my connection string in ssms

    • @DataVids
      @DataVids  2 года назад

      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

    • @namansharma1448
      @namansharma1448 2 года назад

      @@DataVids thanx i got it .amazing video btw

  • @angel_machariel
    @angel_machariel Год назад +1

    Wait a minute. Before you scaffolded and performed a database update, the database itself already exist, you only added the tables to it. Right?

    • @DataVids
      @DataVids  Год назад

      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.

    • @angel_machariel
      @angel_machariel Год назад

      @@DataVids Yes, but in your code, that database did exist already, right? Then you can add tables later to it, that I get.

  • @ariel._.9186
    @ariel._.9186 2 года назад +1

    thank you

  • @user-bk9zs9jr2b
    @user-bk9zs9jr2b Год назад +1

    красава, чувак

    • @DataVids
      @DataVids  Год назад

      Большое спасибо! (Google translate !)

  • @STUPIDYOUTUBE_HIDINGMSGS
    @STUPIDYOUTUBE_HIDINGMSGS 2 года назад

    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!

    • @DataVids
      @DataVids  2 года назад

      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.

  • @LamNguyen-ei6sv
    @LamNguyen-ei6sv 2 года назад

    i am a dog