In this case, the Enrollments property of a Student entity will hold all of the Enrollment entities that are related to that Student entity. In other words, if a Student row in the database has two related Enrollment rows rows that contain that student's primary key value in their StudentID foreign key column , that Student entity's Enrollments navigation property will contain those two Enrollment entities. In the Models folder, create Enrollment. Ordinarily you would choose one pattern and use it throughout your data model.
Here, the variation illustrates that you can use either pattern. In a later tutorial , you'll see how using ID without classname makes it easier to implement inheritance in the data model. The question mark after the Grade type declaration indicates that the Grade property is nullable. A grade that's null is different from a zero grade -- null means a grade isn't known or hasn't been assigned yet.
The StudentID property is a foreign key, and the corresponding navigation property is Student. An Enrollment entity is associated with one Student entity, so the property can only hold a single Student entity unlike the Student. Enrollments navigation property you saw earlier, which can hold multiple Enrollment entities.
The CourseID property is a foreign key, and the corresponding navigation property is Course. In the Models folder, create Course. We'll say more about the DatabaseGenerated attribute in a later tutorial in this series. Basically, this attribute lets you enter the primary key for the course rather than having the database generate it.
The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the Microsoft. In your code you specify which entities are included in the data model. You can also customize certain Entity Framework behavior. In the Data folder create a new class file named SchoolContext.
This code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table. The Entity Framework would include them implicitly because the Student entity references the Enrollment entity and the Enrollment entity references the Course entity. Property names for collections are typically plural Students rather than Student , but developers disagree about whether table names should be pluralized or not.
For these tutorials you'll override the default behavior by specifying singular table names in the DbContext. NET Core implements dependency injection by default. Services such as the EF database context are registered with dependency injection during application startup.
Components that require these services such as MVC controllers are provided these services via constructor parameters. You'll see the controller constructor code that gets a context instance later in this tutorial.
Add using statements for ContosoUniversity. Data and Microsoft. EntityFrameworkCore namespaces, and then build the project. The Entity Framework will create an empty database for you. In this section, you write a method that's called after the database is created in order to populate it with test data.
Here you'll use the EnsureCreated method to automatically create the database. In a later tutorial you'll see how to handle model changes by using Code First Migrations to change the database schema instead of dropping and re-creating the database.
In the Data folder, create a new class file named DbInitializer. The code checks if there are any students in the database, and if not, it assumes the database is new and needs to be seeded with test data. In Program. The first time you run the application, the database will be created and seeded with test data.
Whenever you change the data model:. In later tutorials, you'll see how to modify the database when the data model changes, without deleting and re-creating it. In this section, the scaffolding engine in Visual Studio is used to add an MVC controller and views that will use EF to query and save data. Scaffolding differs from code generation in that the scaffolded code is a starting point that you can modify to suit your own requirements, whereas you typically don't modify generated code.
When you need to customize generated code, you use partial classes or you regenerate the code when things change. That was configured in the Startup. When you started the application, the DbInitializer. EF saw that there was no database and so it created one, then the remainder of the Initialize method code populated the database with data. Right-click the Student table and click View Data to see the columns that were created and the rows that were inserted into the table.
Because you're calling EnsureCreated in the initializer method that runs on app start, you could now make a change to the Student class, delete the database, run the application again, and the database would automatically be re-created to match your change. For example, if you add an EmailAddress property to the Student class, you'll see a new EmailAddress column in the re-created table.
The amount of code you had to write in order for the Entity Framework to be able to create a complete database for you is minimal because of the use of conventions, or assumptions that the Entity Framework makes. For example, you can explicitly specify table names, as you saw earlier in this tutorial. And you can set column names and set any property as primary key or foreign key, as you'll see in a later tutorial in this series.
Some things to be aware of when you are writing asynchronous code that uses the Entity Framework:. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. Any additional feedback?
Prerequisites If you're new to ASP. Visual Studio NET and web development workload. NET 5. Solve problems and troubleshoot If you run into a problem you can't resolve, you can generally find the solution by comparing your code to the completed project.
Tip This is a series of 10 tutorials, each of which builds on what is done in earlier tutorials. NET Core cross-platform development workload Troubleshooting If you run into a problem you can't resolve, you can generally find the solution by comparing your code to the completed project. Submit and view feedback for This product This page. Add a comment. Active Oldest Votes. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
Related Hot Network Questions. Question feed. Can I try the Nadex platform before opening an account? Refer your friends to Nadex! Can I log in to my live and demo accounts at the same time? Can I change my login details? I'm using the correct login details, but nothing is happening? What happens if I've forgotten my login details? What happens if I enter an incorrect password? Which countries does Nadex accept applications from?
Is my personal data safe and private?
0コメント