Entity Framework Code First And Migrations: Part Two

If you are new to this series, kindly acquaint yourselves with part one at:

Introduction

In this second article, we will analyze the use of Entity Framework in relation to the Code-First paradigm, which -- as we saw earlier -- does not need an already-existing database schema. Rather than allowing it to operate at the data level, we start from the definition of the objects/classes that will characterize the entities. Again, it is made of C# used under WinForms, but -- as mentioned in the previous article -- this choice will not affect a different intended use than that which the developer may reasonably request.

Why Code First?

Having evaluated the practical approach of Database First, one wonders, what are the reasons that should lead us to the Code-First paradigm? On the other hand, you can design the database, working directly on the SQL Server (for example) with our scheme, entities, keys and relationships and then delegating the practical Database-First derivation of the classes and their interconnections, while taking the advantage of the GUI that EDMX models provide.

In fact, there are several reasons why Code-First is an appropriate choice. First, it proves to be the only feasible one in the case in which the base data does not exist, but must be created by the application. In this case, the classes will be defined by the developer to represent the template which helps to create the database and its entities. Code-First is optimally an interface with the existing database, and also where the basic data should be accessible, only in part. Code-First allows the declaration of the classes that contain only what we actually need. We have more control on the same database structure, which will be determined by the written code. If you need to apply the changes to the database, we have useful migration tools, redistributable with the Application. Moreover, the code in use is actually  written once, avoiding the over-bloating that the Database-First approach, by its inherent structure, in a certain sense imposes, with its numerous files and the total derivation of the referenced tables.

Although the present article deals with EF 6.1.3, the version 7.0 Code-First becomes the only usable paradigm and therefore, it involves a major advantage for the future.

Database creation

In this section, we will see how to create a database containing several tables, using only the classes we're going to write. Before that, we summarize briefly about referencing Entity Framework in our project, as was done in the previous article, "Introduction to the Entity Framework with C #, Part I (en-US)", to which we refer the reader to the preliminary information necessary to this second part.

Referencing Entity Framework into the project

To reference EF within our project, I.e., make its libraries accessible to the solution, you must first create a new project in Visual Studio, choose the template that we need (in the example, as anticipated, with C # WinForms) and then save the solution (important to avoid alerts being added to EF) and open manage NugGet packages.

packages

In it, we will package the Entity Framework and simply will add to our solution, using the "Install" button. When finished, we will see how one of the project References have been included -- those related to EF.

ef

We are now ready to use the potential of our ORM, regarding the development.

Development setup

As discussed in the previous article, you will need to add an ADO.NET data entity to the project. Click the solution and choose the "New Item" Add voice. We will choose the section "Date," an object of the type ADO.NET Entity Data Model, which we will call again "TechnetModello".

TechnetModello

The creation wizard will continue and this time, we will select as a content model, a blank template prepared for Code-First.

blank

Click the "Finish" button, a class will be created in the project named TechnetModello, extending the type DbContext. This is the basic type, that will allow us to create an initial database (or instantiate the connection to an existing database), allowing the definition of the classes / entities that will be translated into the tables used by the database itself.

Connection string setting

Opening the class Technet Model, we will see it has the following appearance:

  1. public TechnetModello() : base("name=TechnetModello")  
  2. { } 

This is supplied to DbContext below, in  a string in the form "name = TechnetModello". This syntax refers to the connection string to be used as the class is referenced. In place of such a string that can be passed, for example, an entire ODBC connection string is formed by UDL. If you leave it in the format "name = ...", the name of the string to be found in the App.config file is where we will find a section called connectionStrings. It can be differentiated by different connection strings, each having a different name. In our case, "name = TechnetModello" hints that in App.config, there is a named TechnetModello connection string.

Checking the contents of the file, the following snapshot appears:

  1. <connectionStrings>  
  2. <add name="TechnetModello" connectionString="data source=(LocalDb)\v11.0;initial catalog=ArticoloEF02.TechnetModello;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />  
  3. </connectionStrings> 

An instance of SQL Server already exists, or changes the database name you created, which is defined in the connection string by the Initial Catalog property and can be applied in this string. LocalDB is a version of SQL Server Express, specifically created for the developers. It is typically installed with Visual Studio and has many features of the usual versions of SQL Server and allows the programmer to be able to avoid the installation of other instances, and is dedicated to the development machines. In this case, we will use this database engine, limiting ourselves to changing the database name, which will become TECHNET. The connection string in the App.config  is required to use the following code:

  1. <connectionStrings>  
  2. <add name="TechnetModello" connectionString="data source=(LocalDb)\v11.0;initial catalog=TECHNET;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />  
  3. </connectionStrings> 

At this point, you will need to create the first migration, which allows Code-First contexts to upgrade the physical data model, based on the predisposed classes.

Startup migration

Enabled for the project migrations, running from Package Manager Console, education is as follows:

  1. Enable-Migrations 

Console

 Thus, we created the initial migration, with the command given below:

  1. Add-Migration InitialCreate –IgnoreChanges 

At this point, our project will be enriched by the Migrations directory, which will contain a configuration file, called Configuration.cs and from the first migration file, we are now in a position to perform.

migration

We proceed further by typing the Package Manager Console Update-Database education and we wait until the processing is completed.

Once completed, we will be informed of the migration, performed on the database.

migration

To check what happened on the Server side, we can connect to the instance of LocalDB, for example, via SQL Management Studio: We observe how the desired database has been created.

database

The operations related to the migration will be repeated in all those cases in which you need to make the changes to the database structure itself. According to Code-First paradigm, it will be our code to make the changes in the model and not vice versa. We will see a second example of the migration soon, once we have created some significant entities.

Creating entity classes

Now we have a database. Suppose we want to create the two table in it. They correspond, respectively, to an entity that will serve us to store a trivial identifier of the products, while the second will contain some detailed information about the commodity families of the products available. We can operate in the two modes, namely to create a new class file or append our classes to what is already contained in TechnetModello.cs file. For the practical questions, here we will opt for the second way, in order to make it a more maintainable code - in the real Applications; it makes perfect sense that each class has its own characteristic file.

Articoli class

We define a class named "Articoli". As we saw earlier, the class will contain the properties that the database corresponds to as many fields. Let us then suppose, you want to create a very simple class, consisting of a product code, a description and a family code. Therefore, we can write the code, as shown below:

  1. public class Articoli   
  2. {  
  3.     public string Codice   
  4.   {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Descrizione   
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string CodFamiglia   
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }    

We mentioned in the last article, the so-called Data Annotations or the large synthetic instructions, that help in defining the special properties of our fields. To map the structure of the classes in Code-First syntax, they are important, particularly because to the developer, it broadens the definition of the property by specifying additional characteristics of the field, such as its length.

We use then the Data Annotations to delineate the profile of our articles table.

In the following example, we put the key constraint on only the Code field, thereby setting the obligation and by Data Annotation StringLength, that defines a maximum length for each field.

  1. public class Articoli  
  2. {  
  3.     [Key]  
  4.     [StringLength(15)]  
  5.     [Required]  
  6.     public string Codice   
  7.     {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     [StringLength(50)]  
  12.     public string Descrizione   
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17.     [StringLength(6)]  
  18.     public string CodFamiglia   
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.   

Families class

The Families class will contain only two fields: Code family, of course, it's a key and description representative of the long description of the family.

  1. public class Famiglie   
  2. {  
  3.     [Key]  
  4.     [StringLength(6)]  
  5.     [Required]  
  6.     public string CodFamiglia   
  7.     {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     [StringLength(35)]  
  12.     public string Descrizione   
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17.  

TechnetModello class

 The change is made as follows:

  1. public class TechnetModello: DbContext   
  2. {  
  3.     public TechnetModello(): base("name=TechnetModello") {}  
  4.     public virtual DbSet < Articoli > Articoli   
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     public virtual DbSet < Famiglie > Famiglie   
  10.     {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }    

Second migration

With the new model, ready for use, we can now proceed with the publication of the second migration, this time, with the task of creating the tables in the database.

From the Package Manager Console, we perform the instruction gven below:

  1. Add-Migration Tabelle_di_base 

Console

We perform as follows:

  1. Update-Database 

By connecting to the instance of LocalDB, we will see how our tables have been added, in which the final character class name in English was added designates the plural.

This peculiarity, a bit annoying in our language, can be circumvented by a special DataAnnotation, which we will analyze in the context of the upcoming migration.

DataAnnotation

Initial data in migration context

It may happen that, after the release of the model updates, you want to provide the preset table data (think, for example, a ministerial table, to be provided as they are and above all ready for the operation). The migrations allow us to distribute the precompiled data, exploiting the Seed () method, that is located in Configuration.cs files. This method is executed subsequent to the migration and is suitable to perform the various operations on the database.

Following our example, we will want to migrate our application people table's Articles and Families with some data, which will be used for the testing.

The Seed method will thus become as shown below:

  1. protected override void Seed(ArticoloEF02.TechnetModello context)   
  2. {  
  3.     context.Articoli.AddOrUpdate(new Articoli   
  4.     {  
  5.         Codice = "Test001", Descrizione = "Articolo di Test 001", CodFamiglia = "F01"  
  6.     }, new Articoli {  
  7.         Codice = "Test002", Descrizione = "Articolo di Test 002", CodFamiglia = "F01"  
  8.     }, new Articoli {  
  9.         Codice = "Test003", Descrizione = "Articolo di Test 003", CodFamiglia = "F03"  
  10.     }, new Articoli {  
  11.         Codice = "Test004", Descrizione = "Articolo di Test 004", CodFamiglia = "F02"  
  12.     }, new Articoli {  
  13.         Codice = "Test005", Descrizione = "Articolo di Test 005", CodFamiglia = "F02"  
  14.     }, new Articoli {  
  15.         Codice = "Test006", Descrizione = "Articolo di Test 006", CodFamiglia = "F01"  
  16.     });  
  17.     context.Famiglie.AddOrUpdate(new Famiglie  
  18.     {  
  19.         CodFamiglia = "F01", Descrizione = "Prodotti Finiti"  
  20.     }, new Famiglie {  
  21.         CodFamiglia = "F02", Descrizione = "Semilavorati"  
  22.     }, new Famiglie {  
  23.         CodFamiglia = "F03", Descrizione = "Materie Prime"  
  24.     });  
  25. }    

Notice how it uses, as a parameter, a context variable TechnetModello type: This will then allow you to refer directly to our data entities, calling  on the classes representing the tables. These methods allow you to change the underlying data. In our case, we add six items and three families.

We can now use the command again, shown below:

  1. Update-Database 

There are pending migrations, but at the end of this command, Seed () method will always be executed. Therefore, going back to query our database and ask a simple SELECT from the tables created, we get the following result:

result

Data access

In this section, we will see some simple examples to make access to the data in the context of selecting insert, update and delete.

For all these cases, it is necessary to have a data context properly initialized. In our case, this is achieved with the simple definition of a new reference to a variable, which belongs to the class that extends DbContext, namely:

  1. TechnetModello db = new TechnetModello(); 

More generally, if we want to initialize a data context and delegate the task of having this connection upon completion of the operations you want, we can use the using clause, as shown below:

  1. using(TechnetModello db = new TechnetModello())  
  2. {  
  3.     // TO DO: operazioni di lettura/scrittura    
  4. }   

Data querying

Data querying refers to a simple routine to make the cyclic reading.

As it appears to be clear from the premises, our tables can be thought of as the lists belonging to a given type, which is precompiled (existing data in the table) and editable.

Consequently, once it initialized the data context, the entity items can be scrolled as a normal custom.

  1. using(TechnetModello db = new TechnetModello())  
  2. {  
  3.     foreach(Articoli a in db.Articoli)   
  4.     {  
  5.         MessageBox.Show(a.Codice + " " + a.Descrizione);  
  6.     }  
  7.  

The snippet above initializes the data context on the db variable. Within a for / each loop, we read the individual entities of the type contained in Articles db.Articoli list, on the code description screen. In these cases, the LINQ syntax is obviously very useful to run the queries or to target the selections.

For example, suppose you want to extract a type of Articles variable representing the first occurrence of the items that the family code of F02 has.

With LINQ syntax, we can write concisely the following snippet:

  1. using(TechnetModello db = new TechnetModello())   
  2. {  
  3.     Articoli a = db.Articoli.Where((x) => x.CodFamiglia == "F02").FirstOrDefault();  
  4.     if (a != null) MessageBox.Show(a.Codice);  
  5. }   

New record insert

The operations of insertion pass - of course - for the referencing of a type compatible with that of the variable list, append it to the latter, once the properties are enhanced. A new type of Articles variable can be declared as follows:

  1. Articoli a = new Articoli() { Codice = "PROVA", Descrizione = "Articolo inserito da codice", CodFamiglia = "" }; 

Obviously, the DbSet items has an Add () method, which will allow us to pass this variable to the set to add to it. No other controls, however, will expose us to the risk that entered the duplicate keys in the database. Therefore, it becomes necessary, before inserting our variable, to check if its key elements are not already specified.

With LINQ, it all becomes very simple, as follows:

  1. Articoli a = new Articoli()  
  2. {  
  3.     Codice = "PROVA", Descrizione = "Articolo inserito da codice", CodFamiglia = ""  
  4. };  
  5. if (db.Articoli.Find(a.Codice) != null) MessageBox.Show("Articolo già presente su tabella Articoli");  
  6. else db.Articoli.Add(a);   

We defined our type of Articles variable. Let's run the Find () method on the table DbSet representative, looking at any occurrences of a.Codice, or the primary key of the table. In case you find an existing item, we will issue a notice of an inability to proceed, but not to add the element.

However, in the data modification operations, simply do not act on the entities: it is necessary to consolidate the changes or to inform the Entity Framework required to save the changes that occurred during the use of DbSet. DbContext has a method called SaveChanges (),  which precisely performs this consolidation. It also has a property to be used for the control purposes with the changes that occurred on the database. You can then, before performing the save routine, introduce a preliminary check to make sure that the procedure, shown below, is actually necessary:

  1. using(TechnetModello db = new TechnetModello())  
  2. {  
  3.     Articoli a = new Articoli()   
  4.     {  
  5.         Codice = "PROVA", Descrizione = "Articolo inserito da codice", CodFamiglia = ""  
  6.     };  
  7.     if (db.Articoli.Find(a.Codice) != null) MessageBox.Show("Articolo già presente su tabella Articoli");  
  8.     else db.Articoli.Add(a);  
  9.     if (db.ChangeTracker.HasChanges()) db.SaveChanges();  
  10. }   

Since the snippet shown above may not produce variations in DbSet (in case of which a duplicate key is detected), we use the ChangeTracker properties and its HasChanges method to check whether the modifications on DbContext are available or not. If so, you run the SaveChanges method and consolidate current data.

result

Updating existent records

At this point, the update operations and delete data become obvious. Suppose we want to change the newly inserted record or having a key field code = TEST.

The first thing to do is to locate the DbSet items and then - if it is; we can change its properties. This operation will update at the same time the ChangeTracker, by which we will determine whether to perform a data consolidation or not.

Very simply, the code will be as follows:

  1. using(TechnetModello db = new TechnetModello())  
  2. {  
  3.     Articoli a = db.Articoli.Find("PROVA");  
  4.     if (a != null)   
  5.     {  
  6.         a.CodFamiglia = "F03";  
  7.     }  
  8.     if (db.ChangeTracker.HasChanges()) db.SaveChanges();  
  9. }   

In this case, we want to update for the single record, the Family Code, bringing it to F03. Note, that it is sufficient to have the corresponding property setting, when you have found the record. This can of course be performed for all the desired items. The SaveChanges () method must be called only once, in the end for the processing.

result

Record deletion

Even deleting a record is simple: it is to remove a particular item from DbSet items and re-consolidate the change.

In the following snippet, the research article has a key field code = TEST is performed and its subsequent removal, if found:

  1. using(TechnetModello db = new TechnetModello())  
  2.   
  3. {    
  4.     Articoli a = db.Articoli.Find("PROVA");    
  5.     if (a != null) db.Articoli.Remove(a);    
  6.     if (db.ChangeTracker.HasChanges()) db.SaveChanges();    
  7. }   

Table name modification

As a final aspect of this article, we will see how to change the names of the physical tables through Data Annotations.

Additional considerations about it will be made in the future; when we will discuss more closely the so-called Fluent API.

We have seen how, by default, the Entity Framework generates tables giving them a character and queue name, which defines the plural in English. In Italian, we have already declined the names in the plural class, we want to avoid the tables that are created with a different nomenclature than you expect. Enough will precede the declaration of the classes and the items in the Families Data Annotation of the Table, indicating the name that the tables will have to hire.

The TechnetModello files therefore will appear, as shown below:

  1. using System;  
  2.   
  3. using System.ComponentModel.DataAnnotations;  
  4.   
  5. using System.ComponentModel.DataAnnotations.Schema;  
  6.   
  7. using System.Data.Entity;  
  8.   
  9. using System.Linq;  
  10.   
  11. public class TechnetModello: DbContext  
  12.   
  13. {  
  14.   
  15.     public TechnetModello(): base("name=TechnetModello") {}  
  16.   
  17.     public virtual DbSet < Articoli > Articoli {  
  18.         get;  
  19.         set;  
  20.     }  
  21.   
  22.     public virtual DbSet < Famiglie > Famiglie {  
  23.         get;  
  24.         set;  
  25.     }  
  26.   
  27. }  
  28.   
  29. [Table("Articoli")]  
  30.   
  31. public class Articoli  
  32.   
  33. {  
  34.   
  35.     [Key]  
  36.   
  37.     [StringLength(15)]  
  38.   
  39.     [Required]  
  40.   
  41.     public string Codice {  
  42.         get;  
  43.         set;  
  44.     }  
  45.   
  46.     [StringLength(50)]  
  47.   
  48.     public string Descrizione {  
  49.         get;  
  50.         set;  
  51.     }  
  52.   
  53.     [StringLength(6)]  
  54.   
  55.     public string CodFamiglia {  
  56.         get;  
  57.         set;  
  58.     }  
  59.   
  60. }  
  61.   
  62. [Table("Famiglie")]  
  63.   
  64. public class Famiglie  
  65.   
  66. {  
  67.   
  68.     [Key]  
  69.   
  70.     [StringLength(6)]  
  71.   
  72.     [Required]  
  73.   
  74.     public string CodFamiglia {  
  75.         get;  
  76.         set;  
  77.     }  
  78.   
  79.     [StringLength(35)]  
  80.   
  81.     public string Descrizione {  
  82.         get;  
  83.         set;  
  84.     }  
  85.   
  86. }   

At this point, we can generate a new migration, shown blow:

  1. Add-Migration NomiTabelle 

and subsequently run it

  1. Update-Database  

After the migration, we are going to query the database TECHNET  and we will see how the name of the tables have changed, reflecting more hours than desired.

result

Download

The source code used in the examples, mentioned above, can be downloaded.

Conclusion

Here we've briefly outlined the methodologies concerning the Code-First paradigm of Entity Framework, showing some examples of use of the created entities, and focusing on data modeling possibilities underlying the context. We recommend the reader to become familiar with these concepts, together with those of the previous article, and waiting to continue this overview with the upcoming events of the series.

Other languages

The present article is available in the following localizations: