How to use DbContext without using Edmx in C#?

c# dbcontext entity-framework

Question

Can we use DbContext without adding EDMX in project for Data model here is sample code in which I am trying to save Instance class object with the help of 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();
        }
    }
}
1
6
11/6/2014 5:59:23 AM

Accepted Answer

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

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

7
11/6/2014 6:01:58 AM

Popular Answer

follow this

1) create 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 class

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

}

3) DA For query execution

 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;
    }


}

}



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow