EntityFramwork Code First 1

Açıklama kısmını sora yazacağım...

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            UrunEkle();
            using (ShopVeriTabani db = new ShopVeriTabani())
            {
                //category bilgisiniide yazdırmak istiyorsa Include deyimini kullanmalıyız.
                foreach (var item in db.Products.Include("Category"))
                {
                    Console.WriteLine("{0}  {1}  {2} ", item.Name,item.Price,item.Category.Name );
                }
            }
            
        }

        static void UrunEkle()
        {
            using (ShopVeriTabani db = new ShopVeriTabani())
            {
                var myCategory = new Category { Name = "computer" };
                var urun = new Product
                {
                    Name = "Biber",
                    Price = 12.65m,
                    Category = myCategory
                };
                db.Products.Add(urun);
                db.SaveChanges();
            }
        }
    }


    public class Category
    {
        public int CategoryId { get; set; }
        public string Name {get;set;}

        public List<Product> Products { get; set; }
    }

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }

        public Category Category { get; set; }

    }

    public class ShopVeriTabani : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }
}

Add comment

The file '/Custom/Widgets/Calendar/widget.cshtml' does not exist.The file '/Custom/Widgets/Category list/widget.cshtml' does not exist.The file '/Custom/Widgets/Tag cloud/widget.cshtml' does not exist.The file '/Custom/Widgets/Page List/widget.cshtml' does not exist.The file '/Custom/Widgets/Month List/widget.cshtml' does not exist.