> For the complete documentation index, see [llms.txt](https://cnahmet.gitbook.io/asp-net-notlarim/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cnahmet.gitbook.io/asp-net-notlarim/orm-entity-framework-core/veri-tabani-iliskileri.md).

# Veri Tabanı İlişkileri

## One to One (Bire Bir İlişki)

Bire bir ilişki tanımlanacak olan tablolar yada entity ler arasında entity propertyleri ilgili class lara eklenerek ilişki kurulur. İlişki kuracak tabloya, ilişki kurulacak tablonun id si eklenir.

```
public class Kisi
{
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        
        public Musteri Musteri{ get; set; }
}


public class Musteri
{
        public int Id { get; set; }
        public string IdendityNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; } 
        
        
        public Kisi Kisi{ get; set; }
        public int KisiId { get; set; }
        
}

Kisi ve Musteri entity lerimiz var. Her kişi aslında birer müşteri ve her müşteri
de bir kişidir mantığı ile bu iki entity arasında bire bir ilişki vardır.
Kisi class ımıza Musteri classını baz alan bir Musteri alanı ekledik. 
Musteri classına ise asıl tablomuz olan Kisi classından gelen bir Kisi alanı 
ve KısıId alanı ekledik.
```

## One to Many (Bire Çok İlişki)

Bire çok ilişki tanımlanacak olan tablolarda asıl classımıza ilişki kuracak classımızı bir list şeklinde tanımlarken, ilişki kuracak tabloya ise asıl class ın property si eklenir.

```
public class Kisi
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public List<Adres> Adreses { get; set; }
    }

public class Adres
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
        
        public int KisiId{ get; set; }
        public Kisi Kisi{ get; set; }
    }
    
Her adres 1 kişiye aittir. Ama 1 kişinin birden fazla adresi olabilir.
```

## Many to Many (Çoka Çok İlişki)

Çoka çok ilişki kuracak olan classlara kendileri arasında List şeklinde propertylerini tanımlarız. Bu tabloların ilişkilerinin tutulacağı ayrı bir entity ekleyerek ilişkiyi kurmuş oluruz.

```
public class Product 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        
        public List<ProductCategory> ProductCategories { get; set; }

    }

public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public List<ProductCategory> ProductCategories { get; set; }
    }


public class ProductCategory
    {
        public int ProductId { get; set; }
        public Product Product { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }
```

Bu tanımlamaları yaptıktan sonra Context class ında bu tabloların birincil anahtarlarını ayarlıyoruz. ProductCategory classındaki ProductId ve CategoryId iki ayrı birincil anahtar olacak. Bunun amacı birden aynı id lerin tekrar etmemesidir.

```
protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ProductCategory>()
                .HasKey(t=> new {t.ProductId,t.CategoryId});

            modelBuilder.Entity<ProductCategory>()
                .HasOne(pc=> pc.Product)
                .WithMany(p=>p.ProductCategories)
                .HasForeignKey(pc=>pc.ProductId);

            modelBuilder.Entity<ProductCategory>()
                .HasOne(pc=> pc.Category)
                .WithMany(c=>c.ProductCategories)
                .HasForeignKey(pc=>pc.CategoryId);
        }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cnahmet.gitbook.io/asp-net-notlarim/orm-entity-framework-core/veri-tabani-iliskileri.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
