Repository navigation
C Programming Intermediate Lecture 5 Entity Framework in NET Core SQLite DataGrid Binding
Full tutorial link > https://www.youtube.com/watch?v=SukirVOphnI
C# Programming (Advanced/Intermediate) Lectures (5/14) will teach you #CSharp #programming from beginner to intermediate/expert level. This course is an excellent source to learn advanced concepts in C# #coding. If you are a beginner to C#, watch our previous course lectures. The playlist link of the previous course is below.
Full playlist of the previous course: C# For Beginners
https://www.youtube.com/playlist?list=PL_pbwdIyffskoSXySh0MdiayPJsBZ7m2o
Discord ➡️ https://bit.ly/SECoursesDiscord
Patreon ➡️ https://www.patreon.com/SECourses
Technology Playlist (30+ Tutorial / Guide / News / Review Videos)
https://www.youtube.com/playlist?list=PL_pbwdIyffsnkay6X91BWb9rrfLATUMr3
Stable Diffusion Playlist (30+ Tutorial / Guide Videos)
https://www.youtube.com/playlist?list=PL_pbwdIyffsmclLl0O144nQRnezKlNdx3
Please join discord, mention me and ask me any questions. Thank you for like, subscribe, share and Patreon support. I am open to private consulting with Patreon subscription.
Full playlist of this course
https://www.youtube.com/playlist?list=PL_pbwdIyffslHaBdS3RUW26RKzSjkl8m4
You can watch for free our C# Programming, ASP .NET Core MVC programming, Machine Learning and Software Engineering related courses on our playlists page
https://www.youtube.com/@SECourses/playlists
GitHub code repository of this course (source codes of the lectures)
https://github.com/FurkanGozukara/Introduction-to-Programming-2-2021
In lecture 5:
-
What is ADO.NET
-
What is SQLite
-
How to convert MS-SQL database class into SQLite database class
-
How to compose a standalone SQLite database programmatically
-
How to compose a SQLite table programmatically
-
How to insert data into SQLite with parameterized queries
-
How to publish your application in a portable way that can generate SQLite database locally on target computer without any installation
-
What is Entity Framework?
-
Do Entity Framework Model quick wizard available in .NET Core?
-
How to use Entity Framework Model quick wizard with Database first approach in .NET Core
-
How to bind a DataGrid to Entity Framework context and save changes made in datagrid in database?
-
How to refresh DataGrid when bound to Entity Framework context?
Introduction:
In today's technology-driven world, where data is a crucial aspect of almost every organization, databases play a vital role in managing and storing that data. ADO.NET and SQLite are two such technologies that aid in the management of databases. ADO.NET is a technology used for data access, while SQLite is a popular database management system that is easy to use, lightweight, and offers powerful features. In this article, we will explore ADO.NET, SQLite, and how to work with them in .NET Core.
What is ADO.NET?
ADO.NET is a technology used for data access in .NET applications. It provides a consistent way to access data sources, such as databases, XML files, and web services. ADO.NET is built on top of the .NET Framework and consists of a set of classes, interfaces, and data providers that allow developers to connect to data sources, retrieve data, and update data.
What is SQLite?
SQLite is a popular database management system that is used in many applications. It is an open-source, lightweight, and cross-platform database engine that is easy to use and offers powerful features. SQLite is a self-contained, serverless database engine, which means it does not require a separate server to run. It is also highly scalable, reliable, and can handle a large amount of data.
How to convert MS-SQL database class into SQLite database class?
Converting an MS-SQL database class into a SQLite database class involves a few steps. First, you need to create a new SQLite database using the SQLite command-line tool or a SQLite management tool like SQLite Studio. Once you have created the new database, you need to create a new class in your .NET Core project that represents the SQLite database. This class should have the same structure as the MS-SQL database class, but with some minor changes.
For example, SQLite does not support the VARCHAR(MAX) data type, so you will need to change it to TEXT. Similarly, you will need to change the data type for the IDENTITY column to INTEGER PRIMARY KEY AUTOINCREMENT. Once you have made these changes, you can copy the data access code from the MS-SQL database class and modify it to work with the SQLite database class.
How to compose a standalone SQLite database programmatically?
To compose a standalone SQLite database programmatically, you need to use the SQLite ADO.NET provider. First, you need to create a connection string that specifies the name and location of the SQLite database file. Next, you need to create an instance of the SQLiteConnection class and pass the connection string as a parameter. Once you have established a connection, you can create a new database using SQL commands.
-
00:00:00 Hello dear students, welcome to the Lecture 5 of Introduction to Programming II as known
-
00:00:15 as also Advanced Programming with Csharp course.
-
00:00:19 So today's topic is using SQLite and Entity Framework for database operations.
-
00:00:27 So what is SQLite if you wonder, let's start with reading it.
-
00:00:33 SQLite is a small, fast, and embeddable open-source file system based SQL database.
-
00:00:40 It doesn't have a separate server component like traditional databases.
-
00:00:43 Rather, it reads and writes data directly in disk files.
-
00:00:47 The SQLite database is integrated with the application that accesses the database.
-
00:00:53 The SQLite database file format is cross-platform and can be copied between 32-bit and 64-bit
-
00:00:59 file systems.
-
00:01:01 Due to the serverless architecture, developers don't need to install SQLite before using
-
00:01:06 it.
-
00:01:07 All SQLite transactions are fully ACID compliant.
-
00:01:11 That means all queries and changes are atomic, consistent, isolated, and durable.
-
00:01:16 The SQLite source code is public and is free for use for any purpose, commercial or private.
-
00:01:23 So why SQLite in Csharp?
-
00:01:29 Due to its lightweight structure, SQLite is heavily used in embedded software with devices
-
00:01:34 such as TV, mobile phones, cameras, home electronic devices, and so forth.
-
00:01:40 Most mobile and small device databases doesn't need a server component, SQLite is recommended
-
00:01:46 for mobile-based applications.
-
00:01:49 Writing and writing operations in SQLite database are extremely fast, almost 35% faster than
-
00:01:55 any traditional databases.
-
00:01:57 SQLite reduces application cost because content can be accessed and updated by using concise
-
00:02:03 SQL queries instead of lengthy procedural queries.
-
00:02:07 To connect SQLite, no additional database drivers or ODBC configuration are required.
-
00:02:14 Developers just have to download the library and add the data file in their application.
-
00:02:19 Okay, so when do we should use SQLite?
-
00:02:22 When you are developing a standalone application, what I mean is a single person will use, for
-
00:02:31 example, a mobile game and you need to save some data of the game on the phone of the
-
00:02:39 person, or you are developing another application that is standalone, then SQLite is a great
-
00:02:49 choice.
-
00:02:50 However, this is not appropriate for servers where multiple users will access the database.
-
00:03:00 Then you should use MySQL or SQL Server or Oracle and such.
-
00:03:06 Okay, so for developing standalone applications, SQLite is great because you can publish your
-
00:03:19 application with easiness without setup of SQL Server or MySQL Server and such.
-
00:03:27 It can directly work.
-
00:03:29 You see, they are using SQLite even in cameras or TVs and such.
-
00:03:38 So you can also compose SQLite programmatically or you can use SQLite software as well to
-
00:03:48 compose it.
-
00:03:50 So I will use SQLite from here.
-
00:03:53 Let's download it.
-
00:03:55 Okay, there is source code, platform-compiled binaries, there is a window.
-
00:04:06 I wonder if there is some UI for it.
-
00:04:14 Okay, let's compose our own SQLite.
-
00:04:23 To do that, I will start with copy and pasting our Lecture 4 code.
-
00:04:32 We can continue on it as Lecture 5.
-
00:04:38 Okay, let's open our application.
-
00:04:45 By the way, if you wonder what are these two, they are Lecture 13 and Lecture 14 from our
-
00:04:54 previous semester from Object-Oriented Programming.
-
00:04:59 I will show you Entity Framework after SQLite, therefore I have opened them.
-
00:05:08 So let's rename this, I think it is better to rename projects.
-
00:05:16 It won't rename everything, but it's still better than nothing.
-
00:05:22 Okay, so as you remember, we have developed a class to handle our database operations.
-
00:05:37 Since SQLite uses the same TSQL format, we can copy and paste this to use it as SQLite.
-
00:05:51 So let's add a new class to our project and name it as cs-sqlite-db.
-
00:06:14 It is ready and from here I will copy and paste everything.
-
00:06:33 So we need to edit here, delete this and delete a curly bracket here.
-
00:06:43 So what will be the difference?
-
00:06:45 The difference will be using SQLite.
-
00:06:51 For SQLite, we need to add a NuGet package to our project.
-
00:06:58 Okay, SQLite, here not this one though, what we need is this one.
-
00:07:12 We need Microsoft Data SQLite Core and Microsoft Data SQLite is a lightweight Adonet provider
-
00:07:19 for SQLite and this package does not include a copy of the native SQLite library.
-
00:07:25 So what is Adonet?
-
00:07:29 It is also important to know, okay let's read it from another page.
-
00:07:41 The Ado .NET is an important part of the .NET framework.
-
00:07:45 It is a specification that unifies access to relational databases, XML files, and other
-
00:07:51 application data.
-
00:07:53 From the programmer's point of view it is a set of libraries and classes to work with
-
00:07:57 database and other data sources.
-
00:08:00 System.
-
00:08:01 Data.
-
00:08:02 SQLite.
-
00:08:03 Core is an implementation of the Ado .NET specification for the SQLite database.
-
00:08:09 It is a driver written in C sharp language and is available for all .NET languages.
-
00:08:14 So as you see Ado .NET is used to manipulate data in different relational databases such
-
00:08:23 as SQLite, MySQL, or the database of Excel and such.
-
00:08:32 So what we are going to use at this lecture is data.SQL.Lite.Core.
-
00:08:40 So with Ado .NET you can also manipulate database of Excel and if you wonder what is database
-
00:08:47 of Excel, it is, what was the extension, I can't even remember it.
-
00:09:11 Ok let me check, ok not this.
-
00:09:38 Let's say this database.
-
00:09:44 Ok Microsoft Access yes.
-
00:09:45 You can also manipulate data of Microsoft Access database with Ado .NET.
-
00:09:52 So that is what it is.
-
00:09:53 So System.Data or like this, Data.SQL, ok I think this is, System.Data.SQL or it was
-
00:10:09 something else, let me check, Data.SQL.Lite, ok it should be Data.SQL.Lite but, oh they
-
00:10:22 have just arrived, you see there is oladb, oracleclient, common, odbc, sqltypes perhaps,
-
00:10:32 ok sql, sqlclient, sqltype, ok it still not exist in our code, so where do we make mistake,
-
00:10:47 maybe we have added the incorrect, let's check the nuget packages, ok maybe we need to install
-
00:11:04 another one, ok what maybe the difference, ok this package does not include a copy of
-
00:11:24 the Data.SQL.Lite library but what does it mean, maybe oladb, ok there is nothing, so
-
00:11:37 let's install this one, ok this one has 20 million downloads and the other one has, oh
-
00:11:53 I see now it is here, probably that was the reason, and let's delete the previous one,
-
00:12:05 oh this one has 38 million downloads, it says that Microsoft Data.SQL.Lite, SQL.Lite connection
-
00:12:17 but we can't access it, anyway let's try now, oh it was Microsoft SQL, ok, oh data, yes
-
00:12:31 so this is coming from Microsoft Data.SQL, I think let's delete the last one we have
-
00:12:39 installed so this was the same, ok let's delete this, ok now it is still here, however we
-
00:12:58 still can't see the data set, we should be able to see though, ok let me check, we need
-
00:13:10 SystemData.SQL.Lite probably, SQL command, SQL database I think, and let's add Unix
-
00:13:31 SystemData as well, now I think we can use, ok what we need is SQL.Lite connection, ok
-
00:13:45 so let's change our library, let's try this one, ok so this is SystemData.SQL.Lite and
-
00:14:14 the other one was Microsoft SQL.Lite so what we need is rather than Microsoft we need SystemData.SQL.Lite
-
00:14:25 to be used as same as SystemData as we are used to, ok this is installed, ok now we should
-
00:14:44 be able to, ok now it is here and I wonder do we need this is coming from, ok and now
-
00:14:53 we have SQL.Lite connection we just need to replace this with this, ok and we need this
-
00:15:06 SQL.Lite data adapter perhaps, SQL.Lite data adapter yes, ok and let's change that as well,
-
00:15:26 ok and this will be command but SQL.Lite command yes, here and ok SystemData.SQL.Lite, ok we
-
00:15:51 also need to change our parameterized query object which is here so let's change it as
-
00:16:00 well so this will be, this will be SQL.Lite, what was the name of it, ok this will be SQL.Lite
-
00:16:22 type dbtype, ok and this will be command SQL.Lite command, oh it didn't change it, ok and
-
00:16:51 interesting it says maybe this will be, ok SQL.Lite command it is expecting dbtype, oh
-
00:17:03 so this is still dbtype, let's see if any, yeah ok no errors is left so what we did was
-
00:17:14 simply changing SQL to SQL.Lite, ok ok so when our application is getting first started
-
00:17:29 I will check whether our SQL.Lite database object is existing or not, by the way we need
-
00:17:37 also another thing now we don't have any more server so we need to set a new connection
-
00:17:48 string so let's check the connection string here, ok database.db and this is memory, oh
-
00:18:06 by the way you can also use memory as a database but this wouldn't be persisting database of
-
00:18:12 course if you use database as memory, ok let's see someone else also have asked it, ok let's
-
00:18:42 try this as our database source, ok data source data.db, db is the extension let's call this
-
00:18:58 as lecture 5.db or yeah lecture 5.db version 3 I don't know what other versions there are
-
00:19:12 but version 3 is what I have seen so far and now I will get the command of composing database
-
00:19:23 file, csharp so how do we create database and table, ok this one looks more logical
-
00:19:52 ok so it is done like this so how we gonna do that we are going to write a constructor
-
00:20:18 here as public ok so this is the constructor of static class and here if this file does
-
00:20:39 not exist ok if to check that I will add using system.io if file exists and what is the file
-
00:20:55 name so let's give the file name like this read only let's say db file name as is ok
-
00:21:11 I think it also requires static keyword equal to false so equal to false let's check it
-
00:21:31 like this what we need to do is ok sql file sqlite file ok so it is done like this sql
-
00:21:49 connection creates file this file name I will append it to here ok you can also make this
-
00:22:02 as read only ok so it will be like this ok so basically we will check that whether our
-
00:22:21 database file exists or not and then if not exists we will compose it this will be done
-
00:22:29 when the first time this any method anything inside this static class is called in the
-
00:22:37 application then we can compose our tables or whatever we want so let's write a method
-
00:22:47 to compose our tables ok so you will also learn that by the way the commands are pretty
-
00:23:04 much same for sqlite and sql server ok this is the first top of our application so let's
-
00:23:19 say compose tables of sqlite ok and name we can compose table ok so if you wonder how
-
00:23:38 do we compose a table it is pretty easy to figure out by using our sql server management
-
00:23:48 studio
-
00:24:07 and with this method you can generate the sql database on your production environment
-
00:24:17 for example you have just only provided an exe file to your customer then when the first
-
00:24:24 time application is running you can check that the database exists or not if not exists
-
00:24:30 then you can compose the database and tables ok so so basically I will just use the generate
-
00:24:39 scripts create database create table by the way ok I think there is an easier way ok so
-
00:24:49 let's refresh this first we are having some problem ok I will just restart it and then
-
00:25:17 I have to reconnect then let's refresh tables expanding taking some time ok so right click
-
00:25:40 and then there is script table is create the new query ok there can be some specific things
-
00:25:51 related to sql server but overall it is same for sqlite as well
-
00:26:17 it is too slow probably since we are almost at our ram memory limit so I will just close
-
00:26:31 pages here ok some memory we got read ok it is taking really too much time it shouldn't
-
00:26:52 be this much what is using this much memory anyway ok I will close this and some more
-
00:27:10 memory we have I will pause until it generates ok so this is the syntax for Microsoft SQL
-
00:27:30 server let's try it on sqlite I am not sure if they are using the same syntax or not so
-
00:27:50 let's add string by the way with add character I am making this string to be treated as a
-
00:28:00 single line this go means that let's find it I know but I will read you the official
-
00:28:21 description ok sql server management studio always inserts
-
00:28:27 a go command when I create a query using the right click script as menu why what does go
-
00:28:33 actually do ok so it is a batch terminator you can however change it to whatever you
-
00:28:39 want from here so let's see if there is another description the go command is used to group
-
00:28:52 sql commands into batches which are sent to the server together the commands included
-
00:28:57 in the batch that is the set of commands since the last go command or the start of the session
-
00:29:02 must be logically consistent for example you can't define a variable in one batch and
-
00:29:08 then use it in another since the scope of the variable is limited to the batch in which
-
00:29:12 it's defined ok so it is about batching however I will just and the query with like this ok
-
00:29:21 then let's execute the command with equal to this time we will use SQLite DB connection
-
00:29:34 and we will use update insert and our query here ok let's see what happens I'm not sure
-
00:29:51 if it is the same syntax but we will see with debugging
-
00:30:14 so from out of nothing with commands we will compose our data base file and do database
-
00:30:27 operations on it with indexes and such other things so this is a great way of using structured
-
00:30:37 index it variables data in your standalone application so there is no database file here
-
00:30:47 right now there is nothing when I click this I will get into this command with f11 ok here
-
00:30:59 ok so this should have been executed and let's see if we have yeah so SQLite is generated
-
00:31:13 since the constructor is run and our database is here let's continue ok connection open
-
00:31:26 is success nice so our connection open is successful then we will execute not query
-
00:31:35 ok we have an exception so the format was probably incorrect let's see ok it says that
-
00:31:45 SQL logic error near on syntax error ok so we have a syntax error and let's see our ok
-
00:31:58 so it seems like this primary key your own primary has a problem ok so what else now
-
00:32:17 we can do we can open server explorer of Visual Studio and then we can add a connection and
-
00:32:32 we can change it to ODBC data source and then we can
-
00:32:49 perhaps open it from here ok there is connected database so perhaps
-
00:33:08 other no ok there is SQL server database fine maybe this can work let's check it out
-
00:33:23 from our debug folder binary here no didn't see it has failed ok so to use an interface
-
00:33:50 what else we can do maybe this one SQL Server Object Explorer and from here let's see what
-
00:33:59 happens ok it is expecting ok there is ok another name open it and from here ok there
-
00:34:27 is browse ok now so let's find another SQLite interface let's see if there is an interface
-
00:34:54 ok so there is DB browser for SQLite I think we can use this ok so this is official home
-
00:35:03 demo SQLite there is github repository so this is yeah this is open source I think we
-
00:35:10 can use this and from here let's see if they have releases there is my labels ok there
-
00:35:23 is windows release here and yeah I will just use zip no installer for 64 windows here so
-
00:35:32 let's just cancel this ok with this one we can edit our database and then perhaps we
-
00:35:42 may generate the crate of table command as well I'm not sure we will see if it has that
-
00:35:54 capability ok there are some libraries ok here the DB browser for SQLite and from here
-
00:36:04 I will just open because our DB is already generated inside our binary here ok ok you
-
00:36:18 see that it has no tables yet and now let's create a table so the table name so let's
-
00:36:32 copy and paste things here ok you see at the bottom we now see the query command and then
-
00:36:42 I will add field 1 so the field 1 will be student id integer not null and it will be
-
00:36:51 a primary key oh you see this syntax is slightly different that is why our these query has
-
00:37:02 failed and this there are some syntax differences sometimes so this one is mvar character so
-
00:37:15 it is basically text ok and it can be null or not no not null so this is not null and
-
00:37:28 this one is also not null yeah and then we have student email ok so edit as well so this
-
00:37:39 will be a text and not null as well and ismail this is ok there is no relation ok there is
-
00:37:55 binary correlation as well so this will be integer and it will be binary and this will
-
00:38:03 be binary no case reader in utf-16 ok I will let them default there is no foreign case
-
00:38:15 there is no default value right now and nickname let's edit as well as text ok not null let's
-
00:38:31 set a default value for this so this will be by default 0 ok yeah so this was our constraint
-
00:38:44 and ok so you see this is the syntax for our table so I will just replace this with this
-
00:38:55 syntax and now we have a string problem so we can fix it ok so the better way would be
-
00:39:13 adding a text file so I will add a new item just text file we can also add SQL file as
-
00:39:26 well SQL query which is not available anyway let's add this text file as like this compose
-
00:39:47 SQL light table dot SQL ok it is here since I have made the extension as SQL now it is
-
00:40:02 highlighted or let's say styled by default with visual studio I have saved it then I
-
00:40:13 have to copy it to by the way not resource but it was being
-
00:40:23 content I believe and it will be always copy or let's say copy if needed then basically
-
00:40:33 we will execute this one so file reads ok read all text and let's ok so ok now let's
-
00:40:58 retry and see if our query will be successful so this method will return integers and therefore
-
00:41:23 we will get the message alright ok let's ok 0 it says 0 I don't know if it has failed
-
00:41:52 or not ok looks like it didn't fail I believe because I see it now here but for to be sure
-
00:42:07 I will delete the SQLite file and restart the application ok so let's close it ok I
-
00:42:20 also need to close it from here close database ok now I can delete it ok by the way it has
-
00:42:33 shown 0 because there were no rows affected we just simply composed a table and now let's
-
00:42:44 open database to see whether our yeah so our query was successful ok when I modify table
-
00:42:59 I can modify it here when I browse table I can see it here so let's add some values to
-
00:43:07 our like table by the way it is same actually so I will just do add a method to one of my
-
00:43:31 buttons here so there is insert none of students yeah so here let's add records to our ok here
-
00:43:46 we don't use the method so I just need to get method version of this plus here perhaps
-
00:43:57 not sure no ok so let's use it perhaps here we have had some parameterized queries but
-
00:44:16 where was it I am looking for it parameterized queries ok let's convert this into a parameterized
-
00:44:34 query so to convert this into parameterized query we need connection and here ok we need
-
00:44:49 parameterized query object I believe let's see I am not sure which one is which one there
-
00:45:10 is parameter name type of parameter value of parameter and here yeah so let's first
-
00:45:20 add parameter query here and we will add objects to our parameterized query which takes ok
-
00:45:44 it will take parameter name so the parameter name will be equal to student id ok and then
-
00:46:07 it will have type of parameter which will be db type integers there is db type and there
-
00:46:16 is sql db type sql parameter ok I think we need to use this one yeah and then we need
-
00:46:34 the value of course so the value will be like this ok here value of the parameter and this
-
00:46:46 will be added into new query ok object ok now I have added into my list now I can compose
-
00:47:12 other ones if I change values of this in the following code then it wouldn't be new objects
-
00:47:23 because since this is a let's say reference type ok so if I change the values here for
-
00:47:33 example like this equal to let's say something like this ok and we add something like this
-
00:47:48 it won't be it all of them would be same ok because since this is reference type when
-
00:47:58 you add it like this you only adding its reference therefore this is not appropriate way however
-
00:48:06 what we can do is we can simply do it like this so it will be a new instance of this
-
00:48:12 object therefore they will have different references we will see more about references
-
00:48:18 in the next semesters at object oriented programming by the way you can watch our object oriented
-
00:48:25 programming course on our channel on our playlists I think it is somewhere here let's see yes
-
00:48:38 here object oriented programming ok so the second parameter is student name and let's
-
00:48:49 also edit by the way this will be dbtype string here and the value will be equal to here ok
-
00:49:07 now the next thing and then we are going to define our next value and it will be let's
-
00:49:24 copy and paste this first oh same thing and this is the student email and let's add this
-
00:49:38 one as well by the way after each new comment I have added to here because the previous
-
00:49:47 reference is getting lost when I compose a new instance of this object this class and
-
00:49:54 then we have ismail so the ismail is binary type therefore I need to set it as binary
-
00:50:05 and this random next like this is this either 0 or 1 ok let's edit 2 and our final object
-
00:50:22 will be nickname I think the nickname is yeah it is here so it is a little bit different
-
00:50:33 here edit and we have to change the parameter name of course nickname ok then we will set
-
00:51:00 the remaining parameters of our new query which are ok query itself so it will be insert
-
00:51:10 into so our table name is tbrstudents and then we can set column names or we can provide
-
00:51:24 exactly same as the column name count so we can directly edit something like this at values
-
00:51:32 equal to student id by the way if you use this approach you have to provide exactly
-
00:51:42 same number of columns that table have ok yes ok so you can't just leave default values
-
00:51:52 with this approach student email ismail nickname ok this is the query and then ok cs alright
-
00:52:18 ok then I parameterize the query update insert the query object here and that's it now it
-
00:52:27 should work I'm hoping that so let's run the application and see ok I will put a breakpoint
-
00:52:35 here to test it not this one and here ok connection is open command generated command text here
-
00:53:02 and then we add the parameter objects let's go here and when we execute oh we have an
-
00:53:12 error so what could be the unable cast object system into system byte so why does it see
-
00:53:24 it as a byte this is integer but which object has the problem I wonder that
-
00:53:39 ok we can see which object has the problem
-
00:54:08 so maybe we don't provide the value here let me check it
-
00:54:13 parameter name parameter type parameter let's call it parameters let's go parameter so let's
-
00:54:39 call it parameters ok so it has so many options db type parameter name parameter size db type
-
00:55:00 object value parameter name db type parameter name ok so let's just provide parameter names
-
00:55:11 and parameter value I won't provide the parameter type in this case because it seems like we
-
00:55:20 are having some problem with the parameter types on this case and I will just add this
-
00:55:31 ok let's try this way so let's put a breakpoint here and see if it is working or not so basically
-
00:55:49 I didn't provide the parameter type therefore the .NET framework should automatically handle
-
00:55:56 it and let's try it continue ok ok I think it has worked so let's select the data in
-
00:56:14 our yeah yeah now it is working you see we didn't do anything our code is perfectly
-
00:56:22 working fine so now we can continue with removing breakpoints and now it is adding both our
-
00:56:35 you see whenever I refresh it it is getting increased and this can also have indexes I
-
00:56:44 guess let's see modify table ok there are constraints primary key and there is ok we
-
00:56:55 can add unique and we can add check I wonder if we can define index on this ok next different
-
00:57:10 courses topic and I wonder if there is indexes here let me check it in a moment light index
-
00:57:26 yeah there is indexes it looks like so so this is the format ok ok I won't edit yet
-
00:57:41 so let's remove this so remove constraints there is primary key and check and then I
-
00:57:51 key so what is this auto increment yeah we don't need it yet we don't need unique default
-
00:57:59 value check ok anyway now it is working now you have the idea of how to use a local database
-
00:58:11 so if I publish this application I can use it directly ok so let's publish application
-
00:58:19 such as we are delivering our job to our employee so I will publish this how we gonna do that
-
00:58:37 let's right click and publish so when publishing it will include the necessary dll files as
-
00:58:45 well ok so the target will be a folder and folder here so let's publish it into somewhere
-
00:58:58 else because I don't want it to be included in our github repository so publish lecture
-
00:59:06 5 ok and ok so when I click finish ok now there is configuration release target runtime
-
00:59:22 portable so portable means that it will put necessary dll files as well and on whichever
-
00:59:31 the computer I put it it should work either in but since we are using bpf it shouldn't
-
00:59:38 it wouldn't work on linux I guess ok so let's check our code ok here our published file
-
00:59:52 and there is runtimes runtimes are taking not much space so we have you see our sql
-
01:00:02 file publish it we have our dll and we have our you see sqlite and client dll when I run
-
01:00:13 the application ok it is started and when I click compose tables you see database has
-
01:00:23 created and when I insert random numbers now we will see the increase in our sqlite file
-
01:00:32 size ok you see here and you see sometimes there is another file getting composed and
-
01:00:41 getting removed I think it is related to inserting records and you see these applications totally
-
01:00:51 standalone and can be deployed to your your employer so if your employer has requested
-
01:01:02 you to program a standalone software then you can use sqlite if you need database in
-
01:01:10 your software and it will save you from a lot of hassle ok so let's take our notes here
-
01:01:22 and lecture 5 context ok ok in lecture 5 ok so what is adonet adonet is the framework
-
01:01:40 that is being used to provide unified interface for different database management or different
-
01:01:52 files adonet what is sqlite how to convert ok sql database plus into sqlite database
-
01:02:15 class ok how to compose a standalone sqlite database how to compose a sqlite table programmatically
-
01:02:39 ok how to insert data into sqlite or let's say how to insert with parameterized queries
-
01:03:04 you can also do other update insert whatever you want with using our this converted class
-
01:03:13 it is so that much easy you don't nothing is different and you can use all the methods
-
01:03:21 here they should all work without much change maybe you may need to change let's see this
-
01:03:34 parameterized query type on update as well yeah here here you just need to change the
-
01:03:42 same which is just copy and paste here ok oh we had something else as well ok so no
-
01:04:06 not this one so this one will be copied and pasted here ok and you can also use our methods
-
01:04:17 to and how to publish your application in a portable way that can generate sqlite database
-
01:04:38 locally on target computer ok without any installation ok did you do anything else
-
01:05:07 i think we haven't ok now i will show you how to use entity framework ok and what is
-
01:05:19 entity framework ok so the entity framework is a little bit more harder to understand
-
01:05:31 at the beginning and we are going to add a new item to our project and it is inside
-
01:05:46 ok ok i think it is not available on my project at the moment so i need to add entity framework
-
01:06:00 so let's check our get ok there is microsoft and framework core ok so is a modern object
-
01:06:19 database mapper for dotnet it supports linked queries change tracking updates and scheme
-
01:06:24 migrations ifi core works with sql server azure azure sql database sqlite and more
-
01:06:36 mysql postgre sql and other databases through provider plugin api so usually it supports
-
01:06:44 most of the databases this is a mapper for us to write link queue queries and and if
-
01:06:57 you use entity framework it also does provide you sql injection protection and ok so now
-
01:07:10 when i add new item it should be here ok it is not here let's search interesting maybe
-
01:07:39 it is not updated yet i am not sure no none of this ok installed WPF data interesting
-
01:08:03 so we have installed it ok there is also entity framework let's also install this one
-
01:08:24 so this is entity framework 6 version 6 and this was version 5 ok so let's retry ok it's
-
01:08:42 still not here i am hoping that it is not related to a visual studio error because sometimes
-
01:08:52 it is happening ok let's see what's installed entity framework so let's restart our application
-
01:09:04 i have a problem with my mouse it is sometimes double clicking yeah it's really annoying
-
01:09:32 but i have to keep using it on this project i believe it was available
-
01:09:53 so when i click add new item ok yes either that entity model is here so it should be
-
01:10:15 also available here as well so let's see add new item data oh it is not available here
-
01:10:28 so what is the difference between two let's see then we get packages installed in my previous
-
01:10:37 semester project that's framework needs also ok only entity framework but we have installed
-
01:10:48 it here as well yeah and the framework same thing maybe i am right clicking in correct
-
01:11:08 place another new project of course for lecture 5 add new item ok add new item add model not
-
01:11:31 available let's see if someone else has asked yes someone has also asked this problem
-
01:11:50 ok
-
01:12:17 maybe let's look specifically for this
-
01:12:36 package
-
01:12:49 let's look like this
-
01:13:13 oh so it is not working with .NET Core
-
01:13:25 okay I found the hard way you can't make other .NET and the model with an interface with .NET Core but you can with .NET Framework
-
01:13:44 so power tools
-
01:14:04 okay I see it looks like with .NET Core we can't use the wizard however with this one let's see
-
01:14:26 it is reverse engine immigration tools
-
01:14:35 okay let's see
-
01:14:45 let's download this so this is named as EFI Core Power Tools
-
01:15:10 it has been asked many times
-
01:15:15 so get tools and features
-
01:15:44 it says it is not supporting
-
01:16:14 so right now we are looking for a solution to use it with .NET Core rather than .NET Framework
-
01:16:37 okay so same answer
-
01:17:06 so this is also another option
-
01:17:16 copying EDMX files from another project
-
01:17:37 okay maybe we can use that so let's compose a new project and copy the necessary files from there and see if it is working or not I am pretty sure it will work
-
01:17:54 so I will compose a temporary project
-
01:17:59 there is also project here which is from previous year
-
01:18:05 okay so WPF of .NET is not supporting entity framework model
-
01:18:13 therefore I will pick WPF of .NET Framework
-
01:18:18 you see it has double click it again
-
01:18:43 okay once this application is loaded it should be available
-
01:18:48 okay so we right click and we click add new item
-
01:18:56 let's name it as entity model
-
01:19:14 now wizard will appear
-
01:19:17 let's get some idea about entity framework
-
01:19:29 and let's continue
-
01:19:38 okay so
-
01:19:42 entity framework is an object relational mapping or framework that offers an automated mechanism to developers for storing and accessing the data in the database
-
01:19:52 this tutorial covers the features of entity framework using code first approach
-
01:19:57 it also explains the new features introduced in entity framework 6
-
01:20:02 okay so this is using code first approach and so if you wonder what is code first and what is model first let me also explain you them
-
01:20:16 okay
-
01:20:18 code first modeling workflow targets a database that doesn't exist in code first will create it it can also be used if you have an empty database and then code first will add new tables to code first allows you to define your model using C sharp or VB net classes
-
01:20:36 additional configuration can optionally be performed using attributes in your classes and properties or by using a fluent API
-
01:20:44 and what is model first approach
-
01:20:53 model first is great for when you're starting a new project where the database doesn't even exist yet
-
01:20:59 the model is stored in an EDMX file and can be viewed and edited in the entity framework designer in model first you define your model in an entity framework designer then generate sequel which will create database schema to match your model and then you execute the sequel to create the schema in your database
-
01:21:18 the classes that you interact with in your application are automatically generated from the EDMX file
-
01:21:24 and then the database first approach which I prefer to use by the way
-
01:21:31 the database first approach provides an alternative to the code first and model first approaches to the entity data model
-
01:21:38 it creates model codes classes properties DB context etc from the database in the project and those classes become the link between the database and controller the database first approach creates the entity framework from an existing database
-
01:21:54 we use all other functionalities such as the model database sync in the code generation in the same way we use them in the model first approach
-
01:22:03 okay so we are going to use database first approach because our database is ready therefore I am picking if he designer from database and next
-
01:22:16 then I will select my database and server and such okay it is loading
-
01:22:28 okay it said it is not available therefore I will generate a new connection and server name
-
01:22:36 this is retrieving list of servers right now
-
01:22:41 you can also type the server name yourself
-
01:22:45 how you are going to find server name such as this select at server name
-
01:22:52 and this is our server name so let's cancel it and type our server name windows authentication and now the database should appear you see they have appeared school database
-
01:23:04 and okay you see it has automatically generated the connection string okay so the same connection settings in the app setting config at school entities
-
01:23:18 is up to you how you save it and frameworks version 6
-
01:23:23 okay then you pick which features of the database to be included and there is also model namespace and this will be used by us to manipulate database objects
-
01:23:41 so I am picking the tables and we don't have leaves or search procedures and then finish then it will generate the necessary classes and EDMX files and such for us
-
01:23:58 this provides an easiness and security level for programmers
-
01:24:06 however with this approach you may not obtain the best performance
-
01:24:15 therefore if you are working on a performance critical software you should stick with our raw queries
-
01:24:27 also it is harder to get used to entity framework at first but of course by using it over time you can get used to it
-
01:24:40 personally I didn't use much actually I haven't used entity framework in any of my projects so far
-
01:24:49 okay so our EDMX is generated here with the sub elements
-
01:24:57 I wonder if we can add it to our project directly so I will click add existing item
-
01:25:05 and this is in our
-
01:25:12 let's see where is the folder of this
-
01:25:19 okay source reports
-
01:25:24 so it is inside
-
01:25:30 users
-
01:25:34 source reports
-
01:25:37 ppf8
-
01:25:42 okay and here I have to add entity model
-
01:25:47 context model css model designer
-
01:25:56 but it's not saying it's here
-
01:26:00 I also need to add
-
01:26:03 EDMX okay let's find where the EDMX is
-
01:26:09 okay EDMX is here
-
01:26:12 however it has failed to add them
-
01:26:17 add existing item
-
01:26:22 okay so when I pick all yeah now I can select all
-
01:26:34 okay these are the SSL files
-
01:26:58 okay they are getting added
-
01:27:03 not sure if will this work or not but I will say it should work
-
01:27:10 and there can be some other things for example packages config
-
01:27:15 at the framework it should be same here I believe
-
01:27:20 let's see do we have packages config here
-
01:27:30 no and this may be a problem
-
01:27:35 and also we have app config and yes
-
01:27:40 in the app config we have to add
-
01:27:45 connection strings here and here you see
-
01:27:50 so I have to copy and paste this
-
01:27:55 my configuration
-
01:28:10 okay where is app config
-
01:28:18 okay looks like we are going to have some problem because we don't
-
01:28:22 they are not exactly same
-
01:28:27 okay there is app example and there is app config
-
01:28:33 I think I will add app config file as well
-
01:28:38 so I think it should be same here
-
01:28:43 but it has supported one time
-
01:28:53 I am not sure if it will work or not so let's check it
-
01:29:01 app config okay and then packages config
-
01:29:10 this may not work
-
01:29:19 let's see if we can
-
01:29:28 okay it says that entity framework is not available in the target framework currently specified
-
01:29:33 okay so if you want to use entity framework unfortunately
-
01:29:38 you can't use .NET core project and the only way
-
01:29:43 looks like the way that I have
-
01:29:48 so far taught you
-
01:29:53 therefore I am going to delete these unnecessary files
-
01:30:00 okay and this one as well
-
01:30:07 maybe it may be done with code to
-
01:30:13 entity framework but
-
01:30:18 I am not that much expert yet
-
01:30:23 I will just delete the unnecessary .NET packages as well
-
01:30:30 okay let's delete them
-
01:30:40 and let's delete these as well
-
01:30:49 okay for entity framework example I will compose a new project
-
01:30:58 delete these too
-
01:31:07 okay
-
01:31:11 let's run our application to see if there is any errors
-
01:31:19 okay so
-
01:31:22 my entity framework model not available in .NET core
-
01:31:34 okay so many people have asked that so many developers
-
01:31:42 let's close this
-
01:32:08 okay it is true but this feature not available in core and might might answer question but core offers reverse engineering command line
-
01:32:16 okay so with reverse engineering command line it's possible so let's try this ify core power tool
-
01:32:23 we have already downloaded it
-
01:32:37 maybe with that it may work
-
01:32:41 okay software is working
-
01:32:46 okay
-
01:33:03 it says requires the GML editor I will install it
-
01:33:08 meanwhile let's install back the entity framework
-
01:33:14 so with reverse engineering it may be possible but
-
01:33:18 I'm not sure we will see
-
01:33:22 so let's install entity framework you see it has over 100 million downloads
-
01:33:32 okay so it is installing the necessary things
-
01:33:41 I will pause
-
01:33:46 I will pause video until they are installed
-
01:33:52 okay after I have closed Visual Studio instances
-
01:33:57 the installation has been completed
-
01:34:00 and now when we open our project
-
01:34:05 it should be available
-
01:34:08 we are going through all these hardship to
-
01:34:13 use entity framework on .NET core application it is already available in
-
01:34:22 .NET framework application so if you are going to develop an application for Windows you can
-
01:34:28 safely use .NET framework but
-
01:34:31 if you are going to develop an application for Linux as well such as a console application
-
01:34:37 and if you want to use entity framework then you need to go through this
-
01:34:42 okay so now we have ify core power tools option here
-
01:34:47 and I click reverse engineer
-
01:34:51 okay now
-
01:34:55 I will add a database from okay you see the same window appeared
-
01:35:01 and let's copy and paste this
-
01:35:06 okay so cool here okay
-
01:35:13 okay it uses ify core 5
-
01:35:18 okay nice
-
01:35:21 so let's click okay
-
01:35:28 okay on here it says loading database objects
-
01:35:32 okay the tables appeared
-
01:35:35 here we are students
-
01:35:37 it says this table has no primary k yes we haven't defined a primary k yet
-
01:35:44 okay
-
01:35:47 the context name okay school context name space lecture 3
-
01:35:52 and type path sub name tv context
-
01:35:56 okay I will leave this pluralized or singularized object names
-
01:36:01 okay it is about naming
-
01:36:05 so
-
01:36:07 I won't touch this
-
01:36:12 okay I won't check these check marks I don't know how much difference they would make
-
01:36:19 okay just leave everything by default and click okay
-
01:36:28 okay so school context is added to our
-
01:36:32 project and let's see where it is
-
01:36:43 okay by the way it is installing ify core provider package from nuget I believe right now
-
01:36:50 as you can see at the left bottom
-
01:36:56 that is why my visual studio is frozen right now
-
01:37:02 unresponsive
-
01:37:06 I am hoping that obvious is still recording yeah
-
01:37:11 okay
-
01:37:13 so we are waiting until it is installing ify core provider package
-
01:37:25 okay I should have installed this previously
-
01:37:29 and you see it has added microsoft entity framework core and microsoft entity framework core metadata
-
01:37:38 libraries into this school context.cs school context class file
-
01:37:44 you see it has public partial class to school context which inherits db context
-
01:37:51 by the way we will see about these inheritance and other things in the next semester in object oriented lecture
-
01:37:59 and it has a constructor which is empty
-
01:38:07 I can't go bottom let's see what's the status
-
01:38:12 okay it is working
-
01:38:18 okay I think it has installed now they should become available
-
01:38:24 yeah and it has some options by default okay so you see it has db set
-
01:38:31 dbs students this is our table name it is public virtual
-
01:38:36 and there is our model creating relational correlation Turkish CI so this is the
-
01:38:43 correlation from our database
-
01:38:50 it says student email is unicode false
-
01:38:55 okay so this is our school context
-
01:38:59 and there is tbs students yes
-
01:39:03 here so this is our actual class that will objects will come from
-
01:39:10 and there is tbs students
-
01:39:14 yeah this is keyless table these are all automatically generated
-
01:39:20 and there is complete gson and which you see it has put the server name
-
01:39:28 and date table name
-
01:39:33 and this is the database name and other things okay now we can use our entity framework
-
01:39:41 the rest is same for both .NET or .NET core
-
01:39:49 okay so let's do some
-
01:39:56 things with entity framework
-
01:40:03 so with entity framework you can manipulate objects with such easiness and save the changes
-
01:40:13 okay for example I will show you
-
01:40:21 let's add another tab to our project
-
01:40:35 okay so here tab item
-
01:40:47 and here the end of it
-
01:40:51 let's make this as entity
-
01:40:57 framework okay
-
01:41:01 okay here I am going to use a grid or a data grid what was the difference I think
-
01:41:09 I think we should use data grid so data grid is here taking full space
-
01:41:14 what I'm going to do is
-
01:41:18 a moment
-
01:41:22 okay let me resize it okay here
-
01:41:27 okay so inside this data grid I will load our table okay
-
01:41:34 name data grid
-
01:41:39 okay I will add two buttons
-
01:41:45 okay watch this part closely because it may be a little bit harder to understand
-
01:41:53 so this button will load data into our data grid
-
01:42:00 okay and name vtn load
-
01:42:05 okay so first let's load data with our traditional method okay
-
01:42:12 so dt grid students have items
-
01:42:18 source which takes a enumerable collection
-
01:42:24 and
-
01:42:28 I think we can use db connection just select data set
-
01:42:38 okay and here select all from tvx students
-
01:42:45 and here tables 0
-
01:42:54 okay so
-
01:42:59 okay so item source not item source but binding perhaps
-
01:43:10 okay data grid
-
01:43:14 data table let's see how it is done
-
01:43:23 okay as data view yeah yeah I remember now table 0 as data view okay
-
01:43:34 and item source
-
01:43:46 so basically this will populate all the data into our data grid but I will just limit it to top 100
-
01:43:57 I will order by name or do we have anything to order them
-
01:44:08 okay let's order by student id yes
-
01:44:15 100 all from tvx students order by student id
-
01:44:23 okay
-
01:44:25 yes this query is working
-
01:44:28 so let's just stop application and
-
01:44:32 change our query
-
01:44:35 okay
-
01:44:37 so this is data view says creates and returns a link enable data view object
-
01:44:45 with data view we are able to assign it to assign it as data source okay
-
01:44:54 so let's open our top let's load data yes now you see the table is loaded into our data grid and I can even change
-
01:45:05 values however would they will save it right now no it is much harder to save this data it is possible we can iterate through
-
01:45:16 columns and rows of these data grid and
-
01:45:22 update them
-
01:45:24 however
-
01:45:27 it is much harder
-
01:45:30 so what we are going to do is instead of our traditional way
-
01:45:36 we will use
-
01:45:42 entity framework okay
-
01:45:45 so to use our entity framework
-
01:45:50 let's define our
-
01:45:55 context school context my context equal to new school context
-
01:46:03 okay my context that
-
01:46:08 okay I am not very good with
-
01:46:13 entity framework so I will just
-
01:46:18 get some information
-
01:46:27 okay so
-
01:46:29 yeah
-
01:46:34 okay where you see you have to write link queries
-
01:46:47 okay so it will be not verbat
-
01:46:52 order by
-
01:46:55 now
-
01:46:57 here that student id
-
01:47:01 and select
-
01:47:04 top use top 100 I think
-
01:47:08 how do we select top let me check
-
01:47:16 okay there is
-
01:47:20 take yeah let's take
-
01:47:23 so take
-
01:47:28 so this will return
-
01:47:35 let's see db set
-
01:47:41 okay and
-
01:47:48 can I assign it into a data view
-
01:47:53 let's see
-
01:47:56 return I order about TBS students
-
01:48:03 okay so to be able to use it
-
01:48:08 then I need to make changes
-
01:48:12 to my context so
-
01:48:15 if I do using it wouldn't be work for me
-
01:48:20 so
-
01:48:24 yeah to be able to save it I have to define it here
-
01:48:28 and
-
01:48:33 let's see
-
01:48:40 how it is done
-
01:48:59 this data context item source
-
01:49:04 okay this local maybe
-
01:49:11 let's see what does this return
-
01:49:16 I created all TBS students okay this is not what we need
-
01:49:42 okay so
-
01:50:04 okay
-
01:50:22 okay I think this should be the correct
-
01:50:25 search okay so this context load
-
01:50:30 local
-
01:50:38 okay this context sellers seller name sellers at
-
01:50:44 save changes no
-
01:50:50 db suppliers local
-
01:50:53 to binding list
-
01:51:01 okay so there is local to binding list
-
01:51:06 and local
-
01:51:09 binding list no
-
01:51:20 then there is maybe local
-
01:51:25 okay so it says that
-
01:51:42 okay so it can be used as
-
01:52:01 however I need to
-
01:52:03 use the binding list but it is not here
-
01:52:08 because I need only certain number of
-
01:52:14 items to be taken
-
01:52:39 okay
-
01:52:49 if we get all the objects it is possible but we need to get only 13 of them
-
01:52:56 okay binding list order new binding list order orders or this
-
01:53:05 okay someone has also asked this
-
01:53:35 hmm
-
01:53:56 but I need to filter only 13 of them so let's try this perhaps so the context is
-
01:54:11 my context and TBS students will load will load
-
01:54:19 okay
-
01:54:30 maybe we can take it something like this I wonder
-
01:54:44 okay maybe we need to filter local
-
01:54:51 binding list equal to
-
01:54:57 item source
-
01:55:01 then can we filter it
-
01:55:05 where maybe we can filter it or order by perhaps
-
01:55:13 order by
-
01:55:21 student ID take
-
01:55:24 100
-
01:55:27 I wonder if this will work
-
01:55:31 yeah let's see
-
01:55:33 okay so this item source
-
01:55:36 and then we will have saved
-
01:55:39 well let's add a save button and see if it is working or not
-
01:55:44 it is working then great this is so much
-
01:55:50 saving time by the way
-
01:55:54 okay so let's add the same
-
01:55:57 we can do achieve this with traditional ways you can try at home to achieve it with tradition traditional way
-
01:56:05 however it's much easier with entity framework object binding and what we are going to do is simply
-
01:56:14 my context
-
01:56:17 save changes
-
01:56:20 yeah that's it
-
01:56:23 let's try
-
01:56:26 I am not sure if this will work or not but we will test it
-
01:56:32 basically you are writing link you to retrieve data or update data and remove data
-
01:56:44 I will show you other examples as well
-
01:56:49 okay load we have error yes
-
01:57:01 okay so we have a problem
-
01:57:04 let's see
-
01:57:07 so this is db-clouding
-
01:57:13 it says that
-
01:57:18 so what was the error again
-
01:57:22 not the database provider has been configured for the db context let's see why
-
01:57:33 we have actually set up our
-
01:57:37 context on here I believe
-
01:57:40 no not this one
-
01:57:42 here school context lecture 3 yeah db2 versus ui hint
-
01:58:08 let's look for the error
-
01:58:23 maybe we can add use add db context
-
01:58:32 okay no not this one
-
01:58:39 so it was it was reverse engineering
-
01:58:49 okay someone else also asked this problem
-
01:59:09 okay there is on configuring
-
01:59:14 someone else also asked it
-
01:59:44 okay one moment
-
01:59:50 our tools reverse engineer
-
02:00:03 okay someone else asked if people don't know okay and problem
-
02:00:19 does the default constructions constructors let's see where it is
-
02:00:33 okay the default constructor is here so we will remove it and let's try
-
02:00:52 the default constructor is here so let's try
-
02:01:11 the default constructor
-
02:01:25 okay
-
02:01:44 okay
-
02:02:03 container where is the service container
-
02:02:23 okay so
-
02:02:33 okay
-
02:02:59 we need to use I think our db connection
-
02:03:23 let's try like this
-
02:03:30 okay so this is all right on configuring and I wonder when will it be
-
02:03:35 colored
-
02:03:41 yeah now it is getting colored
-
02:03:44 okay so
-
02:03:48 because it does not have a primary key oh I see
-
02:03:55 so since entity framework depends on primary key when modifying collections
-
02:04:06 it is throwing an error therefore
-
02:04:11 I will add an identity key to give your students just for the sake of automatic
-
02:04:20 updates of entity framework
-
02:04:27 so identity keys are unique keys that no two records can have
-
02:04:34 therefore each record can be uniquely defined with an identity key
-
02:04:44 okay we are having some problem
-
02:04:48 you see you always have to search for a solution when you encounter a problem
-
02:04:53 okay so I will just add an insert column here I will set it as
-
02:05:00 by the way after I have done this I have to recompose
-
02:05:08 classes so let's say
-
02:05:11 unique
-
02:05:14 ID
-
02:05:15 I will make it as identity key so how am I going to make it I will set it as primary key
-
02:05:23 and
-
02:05:27 I will make it identity specification is identity
-
02:05:32 yes so it will automatically give keys to the existing records
-
02:05:38 here but this may
-
02:05:42 break my application
-
02:05:45 when doing insert updates
-
02:05:48 and such so you see they are having all different keys now
-
02:05:52 now I have to recompose
-
02:05:58 the update
-
02:06:03 my existing
-
02:06:08 let's say
-
02:06:10 classes okay I think it's automatically now recognizing
-
02:06:15 okay and what will it do
-
02:06:21 okay so our code is done
-
02:06:27 let's okay take this back
-
02:06:34 let's see
-
02:06:40 okay you see now we have unique ID here and it has the key parameters so now when we run the application
-
02:06:50 it should work as expected hopefully
-
02:07:01 okay no data okay no errors but it is not visible
-
02:07:08 so where we have the problem
-
02:07:19 so let's just
-
02:07:22 remove this part perhaps and see what happens okay
-
02:07:36 but we didn't load anything into local maybe that is the cause
-
02:07:49 okay now it returns something empty
-
02:07:54 and
-
02:08:00 local
-
02:08:06 let's see if I can pick
-
02:08:29 let's look at it
-
02:08:41 okay there is local data
-
02:08:49 okay so this is loading everything
-
02:08:55 but we don't have it right
-
02:09:07 okay we don't have the load method
-
02:09:11 why might be
-
02:09:15 okay because we have to add using system data entity
-
02:09:20 and when we edit
-
02:09:24 okay now
-
02:09:41 can we modify load I wonder
-
02:09:47 okay load methods
-
02:10:17 no these are not the things that we need
-
02:10:25 okay
-
02:10:31 so yes okay this is what we need
-
02:10:44 okay load yeah
-
02:10:50 okay and that is fine oh I see
-
02:10:54 so if we use find
-
02:11:02 context entry block collection
-
02:11:06 query
-
02:11:09 we can see so with this way we can use
-
02:11:15 okay find
-
02:11:26 connection query where
-
02:11:42 where
-
02:11:46 let's say order by
-
02:11:50 take
-
02:11:55 yes now it's working okay this is what we need exactly
-
02:12:01 and then this okay now it should work
-
02:12:07 all right you see it is harder to
-
02:12:14 grasp the details of
-
02:12:19 entity framework at first but once you get used to it
-
02:12:25 it should work oh by the way we have ordered by student id therefore
-
02:12:30 it may be slower we should have ordered by unique id
-
02:12:37 okay this is taking too much time yeah sql is working
-
02:12:47 yeah let's order by another thing
-
02:12:54 I think I will order by unique id yeah now we should be much
-
02:12:59 faster because primary keys are already sorted
-
02:13:04 therefore
-
02:13:08 it will take the ordering
-
02:13:12 by the way you can also see the queries that is getting
-
02:13:17 executed with profiler
-
02:13:22 so let's open a profiler
-
02:13:29 okay and I'm having memory issues by the way
-
02:13:35 that is why it is too slow
-
02:13:50 I really need more RAM memory on this laptop
-
02:13:57 I will just close it because it is taking
-
02:14:02 too much time where is the profiler
-
02:14:07 profiler here
-
02:14:12 okay let's just restart our use of sql server
-
02:14:17 to free up some memory
-
02:14:22 oh it didn't free any memory
-
02:14:28 okay I will close this
-
02:14:35 and yeah that's
-
02:14:39 more data I'm hoping that this will be faster okay
-
02:14:44 now they are loaded by sorted by unique id as you can
-
02:14:49 see now you cannot change unique id by the way because
-
02:14:54 it's the primary key so I will just
-
02:14:59 set primary id2 to purkan
-
02:15:04 as student name okay and now it is not saved
-
02:15:09 in the database but when I click save and refresh the grid
-
02:15:14 it should get saved
-
02:15:19 so let's try
-
02:15:24 interesting
-
02:15:34 it is automatically saved perhaps but I'm not sure
-
02:15:39 so let's download
-
02:15:44 item source so I need to refresh table I wonder
-
02:15:49 let's see
-
02:15:54 after I click load does it refresh it
-
02:15:59 hmmm
-
02:16:04 perhaps
-
02:16:09 yeah let's make it clear then set the source
-
02:16:14 let's see
-
02:16:19 by the way we will see that if it is saved or not but it should
-
02:16:24 save it so how does
-
02:16:29 entity framework do that it is generating an automatic query
-
02:16:34 I will show you okay loaded
-
02:16:39 and I click again okay hmmm
-
02:16:44 yeah when the item source is in
-
02:16:49 okay so I have to remove item source
-
02:16:54 I believe
-
02:16:59 remove item source
-
02:17:04 remove item source perhaps
-
02:17:09 how do we remove the item source
-
02:17:14 let's see
-
02:17:19 oh there is items
-
02:17:24 oh there is items refresh yeah we don't need any of them
-
02:17:29 let's just refresh
-
02:17:34 after reload
-
02:17:54 okay okay loaded let's check the edit too
-
02:17:59 yes it is saved let's change it let's reload
-
02:18:04 okay once we reload it is not getting
-
02:18:09 reloaded
-
02:18:14 it is not getting saved automatically but not being reloaded as well
-
02:18:19 hmmm
-
02:18:24 hmmm
-
02:18:29 hmmm
-
02:18:34 maybe I need to set the item source null
-
02:18:39 let's try that way
-
02:18:44 hmmm
-
02:18:56 maybe it is related to context because
-
02:19:01 hmmm when we do reload
-
02:19:06 maybe it is not clearing local
-
02:19:11 loaded okay they are loaded
-
02:19:16 change something reload no not changing
-
02:19:21 so let's see so it must be related to
-
02:19:26 students local so I need to
-
02:19:31 yeah I need to I think refresh
-
02:19:36 local of items
-
02:19:41 context so how do I gonna change the local
-
02:19:46 okay okay
-
02:19:51 and it is only get okay there is clear
-
02:19:56 yes let's clear the local and try again
-
02:20:01 okay
-
02:20:06 okay
-
02:20:11 okay
-
02:20:16 okay
-
02:20:21 okay let's see
-
02:20:26 okay now it is gone permanently
-
02:20:31 hmmm
-
02:20:36 hmmm
-
02:20:41 hmmm
-
02:20:46 hmmm
-
02:20:51 hmmm
-
02:20:56 I think I know the reason probably I can do a new
-
02:21:01 context each time when I click load
-
02:21:06 and it should be
-
02:21:11 work
-
02:21:16 because this will reinitialize the context each
-
02:21:21 time I do a load and all the changes
-
02:21:26 made will be removed
-
02:21:31 okay and okay it is here so let's reload
-
02:21:36 yes now working let's make another change and let's type my
-
02:21:41 surname first save it and then load okay now
-
02:21:46 working as expected okay great so how does it work
-
02:21:51 let's see the queries that is being executed
-
02:21:56 let's open the SQL profiler
-
02:22:01 and I will open tuning to see okay so when I
-
02:22:06 load let's see what happens so here the
-
02:22:11 query so you see it has it is selecting top
-
02:22:16 p0 parameter which is 100 here as you can see
-
02:22:21 and the unique ID T is mail and where is what is TT is
-
02:22:26 TBS students you see I can execute this exactly as here
-
02:22:31 and it should work yes so let's see how does it say
-
02:22:36 to save it I will change the unique ID 43
-
02:22:41 let's set it as Toros
-
02:22:46 and save it okay let's see the save command and
-
02:22:51 this is select
-
02:22:56 and no
-
02:23:13 it has done some
-
02:23:20 here here but it has done
-
02:23:25 let's see so it has set
-
02:23:30 no count on update TBS to the set student name equal to p0
-
02:23:35 because we have only changed student name where unique ID is p1
-
02:23:40 so it is expecting a unique ID to work if
-
02:23:45 you are working with entity framework however may it work
-
02:23:50 on it may also work on tables where you don't have
-
02:23:55 unique ID but then you cannot bind it as a source okay
-
02:24:00 if you want to bind it as a source then you have to have a unique ID otherwise it
-
02:24:05 doesn't work and here you see the p1
-
02:24:10 is equal to integer value as 33
-
02:24:15 because it is the word and this is the Toros so let's change
-
02:24:20 multiple values and see the update for example let's set this
-
02:24:25 as Furkan Gözükara and furkan.gözükara
-
02:24:30 at toros.tr and nickname as
-
02:24:35 csca furkan and I will even change my
-
02:24:40 student ID because I am not bound to
-
02:24:45 it and when I say you will see the query okay
-
02:24:50 now the query is expanded with other updated things like here
-
02:24:55 we can even update multiple columns for example let's set this
-
02:25:00 something like this this I mean multiple records this
-
02:25:05 here and here here whatever I want
-
02:25:10 I will just put some values and when I say let's see what happens
-
02:25:15 okay you see each one of the changes are
-
02:25:20 executed individually okay
-
02:25:25 and no oh it is executed like this it is
-
02:25:30 kind of different okay I see so it is
-
02:25:35 executing an extended stored procedure
-
02:25:40 stored procedures are other things related to database
-
02:25:45 and it is providing the parameters of
-
02:25:50 stored procedure as this and they are all getting updated okay
-
02:25:55 all right I think this week
-
02:26:00 I will end it here and next week I will show some more queries related to
-
02:26:05 entity framework so
-
02:26:10 ...
-
02:26:15 ...
-
02:26:20 ...
-
02:26:25 ...
-
02:26:30 ...
-
02:26:35 ...
-
02:26:40 ...
-
02:26:45 ...
-
02:26:50 ...
-
02:26:55 ...
-
02:27:00 ...
-
02:27:05 ...
-
02:27:10 ...
-
02:27:15 ...
-
02:27:20 ...
-
02:27:25 ...
-
02:27:30 ...
-
02:27:35 ...
-
02:27:40 ...
-
02:27:45 ...
-
02:27:50 ...
-
02:27:55 ...
-
02:28:00 ...
-
02:28:05 ...
-
02:28:10 ...
-
02:28:15 ...
-
02:28:20 ...
-
02:28:25 ...
-
02:28:30 ...
-
02:28:35 ...
-
02:28:40 ...
-
02:28:45 ...
-
02:28:50 ...
-
02:28:55 ...
-
02:29:00 ...
-
02:29:05 ...
-
02:29:10 ...
-
02:29:15 ...
-
02:29:20 ...
-
02:29:25 ...
-
02:29:30 ...
-
02:29:35 ...
-
02:29:40 ...
-
02:29:45 ...
-
02:29:50 ...
-
02:29:55 ...
-
02:30:00 ...
-
02:30:05 ...
-
02:30:10 ...
-
02:30:15 ...
-
02:30:20 ...
-
02:30:25 ...
-
02:30:30 ...
-
02:30:35 ...
-
02:30:40 ...
-
02:30:45 ...
