6

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

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

Comments

7

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


}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.