Google Analytics İzleme

26 Aralık 2011

ORM İle Database İşlemleri



İşlem Türü EF 4.0 ile EF 4.1 ile
Tablonun sql sorgusu
/****** Object:  Table [dbo].[Blogs]    Script Date: 12/26/2011 16:25:44 ******/SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Blogs]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Blogs](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [BloggerName] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_Blogs] PRIMARY KEY CLUSTERED (
    [Id] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
 IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)END
GO
SET IDENTITY_INSERT [dbo].[Blogs] ON
INSERT [dbo].[Blogs] ([Id], [Title], [BloggerName]) VALUES (1, N'title1', N'bloggername1')
SET IDENTITY_INSERT [dbo].[Blogs] OFF
Entity Sınıfı tanımlama
using System;
    using System.Collections.Generic;
    
    public partial class Blog
    {
        public Blog()
        {
            this.Posts = new HashSet<Post>();
        }
    
        public int Id { get; set; }
        public string Title { get; set; }
        public string BloggerName { get; set; }
    
        public virtual ICollection<Post> Posts { get; set; }
    }

Connection String
<connectionStrings>
       <add name="BlogDataEntities" connectionString="metadata=res://*/Models.BlogModel.csdl
|res://*/Models.BlogModel.ssdl
|res://*/Models.BlogModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;
data source=.\sharepoint;attachdbfilename=|DataDirectory|\BlogData.mdf;integrated security=True;
user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />
  </connectionStrings>
İşlemleri Yapan Context sınıfı
using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class BlogDataEntities : DbContext
    {
        public BlogDataEntities()
            : base("name=BlogDataEntities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

Listeleme İşlemi
 

private BlogDataEntities db = new BlogDataEntities();

public ViewResult Index()
        {
            return View(db.Blogs.ToList());
        }
Kayıt Arama
 
private BlogDataEntities db = new BlogDataEntities();
public ViewResult Details(int id)
        {
            Blog blog = db.Blogs.Find(id);
            return View(blog);
        }
Yeni Kayıt Ekleme
private BlogDataEntities db = new BlogDataEntities();
[HttpPost]
        public ActionResult Create(Blog blog)
        {
            if (ModelState.IsValid)
            {
                db.Blogs.Add(blog);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(blog);
        }

Kaydı Güncelleme
private BlogDataEntities db = new BlogDataEntities();
[HttpPost]
        public ActionResult Edit(Blog blog)
        {
            if (ModelState.IsValid)
            {
                db.Entry(blog).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(blog);
        }
Kayıt Silme
private BlogDataEntities db = new BlogDataEntities();
[HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Blog blog = db.Blogs.Find(id);
            db.Blogs.Remove(blog);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


Hiç yorum yok: