Once we've got all of this in place, we can start binding the model to the database using FluentAPI.

Creating the Model using FluentAPI

FluentAPI uses a method-based syntax rather than the attribute-based syntax we saw in the previous post. This gives us a bit more control over the properties of the database that will get created, but can be a bit more difficult to read.

To get started, let's override the OnModelCreating method in CourseCatalogContext:

Next, we need to tackle the other properties of Student: FirstName and LastName. Both of these properties will need a maximum length of 100 characters and cannot be blank. Since we are defining characteristics of these properties, we can chain these methods like so:

Setting Up Relationships

The last part of the model definition is to set up the relationships between the entities. In our model, we have the following relationships:

Student 1-to-0..1 StudentAddress

Teacher 1-to-n Course

Student n-to-m Course

Let's start with Student and Student Address.

FluentAPI's naming conventions help us out when defining these relationships. Once we've targeted the entity we want to create a relationship for, we can call methods beginning with "Has" to define the relationship on that side. In the case of Student-StudentAddress, we'd use the method HasOptional, since a Student doesn't have to have a related StudentAddress.

modelBuilder.Entity<Student>()
.HasOptional(x => x.Address)

However, as with all relationships, there are two sides to the story and we still need to define the other side. In order to do this, we use methods starting with "With"; in our case, we want WithRequired because a StudentAddress must have a related Student. The final code that sets up the Student-to-StudentAddress relationship is:

Now we can make the relationship between Teacher and Course. One teacher can have many courses, and any course must have a related Teacher. Therefore, we need the aptly-named methods HasMany and WithRequired:

There's still a piece missing though; you may remember from the class definitions above that Course has a property TeacherID, which is a foreign key to the Teacher table. We need to explicitly define that property as a foreign key, and we do this by using the HasForeignKey method like so:

Two relationships down, one to go! The last one is a little more difficult, because it's a many-to-many relationship.

Remember that in this kind of relationship in a database, you need a mapping table that related the two tabled together. Entity Framework is smart enough to not count that table as an entity unto itself, rather it just reads it as a relationship between two other entities.

Our relationship between Student and Course can be created using HasMany and WithMany:

Lots of people will split these calls out into separate mapping files (this is what Entity Framework Power Tools does) as that is more maintainable for larger projects.

Now we're done! We've got our model and database setup. Don't forget to check out the sample project on GitHub, and feel free to let me know if this tutorial helped you in the comments!

There's still one more post in this series, and that's going to show how we can use Code-First Migrations to manage changes to our database schema, all without writing any SQL! Stay tuned for the last installment of Entity Framework for Beginners!