How can I use DbContext without using Edmx in C #?

Can we use DbContext

without adding EDMX to the project for the data model here is a sample code in which I am trying to save a Instance

class using ContextManager

which is DbContext

.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Linq;
using System.Text;

namespace DbContext_test
{
    public class ContextManager : DbContext
    {
        public ContextManager(string connstring)
            : base(connstring)
        {
        }

        public override int SaveChanges()
        {
            //TODO: Write code before saving dataEntity object or fire some event to do so.
            return base.SaveChanges();
        }
    }

    public class Instances : EntityObject
    {
        public int ID { get; set; }
        public string InstanceCode { get; set; }
        public string InstanceName { get; set; }
    }

    public class InstanceManager
    {
        readonly string ConnectionString;

        public InstanceManager(string connString)
        {
            ConnectionString = connString;
        }
        public void SaveInstance(int id, string instanceCode, string instanceName)
        {
            SaveInstanceInternal(new Instances { ID = id, InstanceCode = instanceCode, InstanceName = instanceName });
        }

        public void SaveInstance(Instances instance)
        {
            SaveInstanceInternal(instance);
        }

        private void SaveInstanceInternal(Instances instance)
        {
            var contextManager = new ContextManager(ConnectionString);

            contextManager.Entry(instance);
            contextManager.SaveChanges();
        }
    }
}

      

+3


source to share


2 answers


You can do this using the first code approach instead of the edmx approach.



http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx

+5


source


follow this

1) create a context class

 public class SchoolPlusDBContext : DbContext
{
    public SchoolPlusDBContext()
        : base("name=SchoolPlusDBContext")
    {

    }


    public DbSet<CategoryMaster> CategoryMaster { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    } 

}

      

2) create a class



public class CategoryMaster
{
   [Key]
    public long CategoryID { get; set; }
   [Required]
    public string CategoryName { get; set; }
     [Required]
    public string CategoryType { get; set; }

}

      

3) DA To execute the request

 public class CategoryDA
{

    SchoolPlusDBContext dbContext = new SchoolPlusDBContext();

    public List<CategoryMaster> GetAllCategory()
    {
        return dbContext.CategoryMaster.OrderByDescending(t => t.CategoryID).ToList();
    }

    public bool AddCategory(CategoryMaster master,string UserName)
    {
        try
        {
            master.CreatedBy = UserName;
            master.CreatedOn = System.DateTime.Now;

            dbContext.CategoryMaster.Add(master);
            dbContext.SaveChanges();
        }
        catch
        {
            return false;
        }
        return true;
    }


}

      

}

+4


source







All Articles