Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Entity Framework Basics Extending Our Entity Data Model Defining a Many-to-Many Relationship with an Explicit Bridge Entity

Jannung Kusdiantoro
Jannung Kusdiantoro
6,743 Points

the concept

i still did not quite understand how the class was built, can you tell me the easy way how to set a one-to-one relationship and one-to-many relationship?

4 Answers

James Churchill
STAFF
James Churchill
Treehouse Teacher

Jannung,

The video that you link to is about creating an explicit bridge table for a many-to-many relationship, though your question is about how to define a one-to-one or one-to-many relationship. Is it how to define relationships in general that you're still struggling a bit with?

It might help to talk through an actual example, preferably something you're familiar with. Can you think of a real-world collection of something that we could work up a data model for?

Thanks ~James

James Churchill
STAFF
James Churchill
Treehouse Teacher

Jannung,

To try to answer your question "...what class or instance do i need to create a database? And why do we need that?"

Entity Framework (EF) isn't absolutely necessary in order to work with data in a .NET application, but developers often find it helpful to use. EF is what is known as an ORM (Object Relational Mapper).

ORMs map data (typically from a database) to objects in your application. The "objects" in this case are often referred to as "entities". In EF, entities are just plain old C# classes. Here's an example of a "Person" entity class.

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

In order to use the entity to retrieve and persist data to a database, we need what is known as a database context. The context does all of the heavy lifting when using EF. Here's an example of a database context class.

public class Context : DbContext
{
    public DbSet<Person> People { get; set; }
}

All of these details are covered in the Entity Framework Basics course. You might also find it help to familiarize yourself with relational databases, by taking our SQL Basics course.

https://teamtreehouse.com/library/sql-basics

I hope that this helps.

Thanks ~James

Jannung Kusdiantoro
Jannung Kusdiantoro
6,743 Points

The video on the link was the last video (so far) i've watch. But i still did have a problem how you put that code, I need an explanation about what code/syntax should i provide to write the database. I'm interested in this project.

Jannung Kusdiantoro
Jannung Kusdiantoro
6,743 Points

By syntax/code, what class or instance do i need to create a database? And why do we need that?