Hasan Akpürüm
blog-post-1

c# EF Data Access Layer Örneği

public int InsertRecord(Customer record)
{
    using (testEntities = new TestEntities())
    {
        testEntities.AddToCustomer(record);
        testEntities.SaveChanges();
    }
    return record.CId;
}
public bool UpdateRecord(Customer record)
{
    EntityKey key;
    object originalItem;

      using(testEntities = new TestEntities())
      {
        key = testEntities.CreateEntityKey("Customer", record);

        if(testEntities.TryGetObjectByKey(key, out originalItem))
        {
            testEntities.ApplyPropertyChanges(key.EntitySetName, record);
        }

        testEntities.SaveChanges();

        return true;
    }
}
public List<customer> GetRecord(Customer record)
{
    testEntities = new TestEntities();
    IQueryable<customer> custQuery = testEntities.Customer.AsQueryable<customer>();
    if (record.CId > 0)
    {
        custQuery = custQuery.Where(c => c.CId == record.CId);
    }
    if (!string.IsNullOrEmpty(record.CFirstName))
    {
        custQuery = custQuery.Where(c => c.CFirstName.Contains(record.CFirstName));
    }
    if (!string.IsNullOrEmpty(record.CLastName))
    {
        custQuery = custQuery.Where(c => c.CLastName.Contains(record.CLastName));
    }
    if (!string.IsNullOrEmpty(record.CAddress))
    {
        custQuery = custQuery.Where(c => c.CLastName.Contains(record.CAddress));
    }
    return custQuery.ToList();
}
public bool DeleteRecord(Customer record)
{
    using (testEntities = new TestEntities())
    {
        var cust = testEntities.Customer.FirstOrDefault(c => c.CId == record.CId);
        testEntities.DeleteObject(cust);
        testEntities.SaveChanges();
        return true;
    }
}

Yorum Yaz