Sayfalar

EntityFramework etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
EntityFramework etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

30 Ağustos 2019 Cuma

NLayered Proje İşlemleri


1- Boş bir Solution Açılır.
2- Add>NewProject>ClassLibrary(.NET Framework) >Projeİsmi.Entities
3- Add>NewProject>ClassLibrary(.NET Framework) >Projeİsmi.DataAccess
4- Add>NewProject>ClassLibrary(.NET Framework) >Projeİsmi.Business
5- Add>NewProject>ClassLibrary(.NET Framework) >Projeİsmi.ADesktopUI


1- Entities, DataAccsess, Business projeleri altına, "Abstract" ve "Concrete" klasörleri oluşturulur.
2- DataAccess Projesine EntityFrameWork NugetPaketi yüklenir.

3- Projeİsmi.WebFormUI>App.Config içerisine connectionString eklenir. Proje Build edilir.



Konun Product ve NorthwindContext üzerinden özeti:

1- EntitiesProjesi> Concrete> Product clasını oluştur. DB ile aynı propları ver.
2- EntitiesProjesi> Abstract> IEntity Interface'i oluştur. Product:IEntity yap.

3- DataAccessProjesi> Concrete>EntityFramework klasörü ekle.( İleride farklı DB eklenmesine yönelik tüm DB operasyonları ayırıyoruz)
4- DataAccessProjesi> Concrete>EntityFramework >NorthwindContext sınıfını oluştur. :DBContexten inherit et.

5- NorthwindContext> DbSet<Product>Produtcs{get;set;} ile entity tanımla
6- DataAccessProjesi>Abstract> IEntityRepository interface ekle.( Db'den bağlandığımız tüm Entitiylerde yapacağımız ortak işlemleri buraya tanımla Add,Update..)
7- DataAccessProjesi> Concrete>EntityFramework> EF Context'i ile Entityleri bağlamak için EFContextRepository<TContext,TEntitiy> olacak şekilde bir base sınıf oluştur.
8-Ardından EFContextRepository<TContext,TEntitiy>  : IEntityRepository<TEntity> şeklinde bu ikisini bağla ve where TEntity: class.. vb. ile Genericlerin durumunu netleştir. İmplemanstasyonu yap operasyonları kodla.
9- DataAccessProjesi> Concrete>EntityFramework >EFProductDal sınıfını oluştur.
10- DataAccessProjesi>Abstract> IProductDal interface oluşutur. :IEntityRepository<Product> olarak inherit et.  
11- EFProductDal : EFContextRepository<NorthwindContext,Product> , IProductDal olarak inherit et.

11- BusinessProjesi>Concrete>ProductManager sınıfı oluştur.
12- BusinessProjesi>Abstract>IProductService interface oluştur. Businenss katmamnında kullanılacak kodları belirt
13- ProductManager:IProductService , kodları impl. et ve içini doldur.
14- ctor ile ProductManager her newlendiğinde yanında birde IProductDal gelmesini sağla. Bu IProductDal kodların içinde kullanarak DB ile bağlantıyı sağlayacaktır.

15- ADesktopUI>NugetPackage>EFW install et aksi halde hata alırsın.
16- ADesktopUI> Listeyi datagridView'e doldurmak için bir tane IProductServis tanımla ve Form1 ctor'unda ProductManager( new EFProductDal()) ile değişkeni ata.
17- Form Load'da productservis.GetProducts() ile Listeyi edin.


1- Business Katmanında ValidationRules klasörü oluşturulur ve  ADesktopUI'dan gelen kullanıcı dataları bu katmanda Fludent Validation ile kontrol edilir.!




















29 Ağustos 2019 Perşembe

Entity Framwork Proje Implemantasyonu


1- Install EntityFramwork Nuget Packed
2- New Class >public  "Nortwind"Context > Inherit from : DBContext
3- In Class > set DbSet<TEntity> TableName(s) {get;set;}
4- Generate  DbSet<TEntity> as a new class (Product etc)
5- Write same properties of Product class from Db Table' fields
6- Write connectionString in App.config file after <conficSections> between <configration>

Not: connectionString UI içeren Projedeki App.Config içerisine kaydeilmeli aksi halde Veri tabanında yeni bir DB oluşturur.


  <connectionStrings>
    <add name="NorthwindContext" 
         connectionString="Data Source=.\sqlexpress;
         Initial Catalog=Northwind; 
         Integrated Security=True; 
         User Id=sa Password=H1978g2808;" 
         providerName="System.Data.SqlClient" />
  </connectionStrings>

28 Ağustos 2019 Çarşamba

Entity FrameWork Add-Update-Delete


public class ProductDal : IProductDal
 {
     public void Add(Product product)
     {
         using (ETradeContext context = new ETradeContext())
         {
 
             context.Products.Add(product);
 
             context.SaveChanges();
 
         }
     }
 
     public void Delete(Product product)
     {
         using (ETradeContext context = new ETradeContext())
         {
 
             var productInDb = context.Entry(product);
             productInDb.State = EntityState.Deleted;
             context.SaveChanges();
         }
     }
 
     public void Update(Product product)
     {
         using (ETradeContext context = new ETradeContext())
         {
 
             var productInDb = context.Entry(product);
             productInDb.State = EntityState.Modified;
             context.SaveChanges();
 
 
         }
     }

MVC LOADING TOO SLOW ISSUE