none
auto-generated code (DBContext) RRS feed

  • Question

  • Hi all. I'm sure the question has been asked.

    I have to alter the auto generated code from DBContext each time I refresh against the database. Its for two reasons, one for a connection string change (dynamic) and the other so I can deep clone a record and need the serialize tag.

    Is there a way I can save the current additional info I add? I currently always copy and paste each time after a refresh from SQL Server.

    Thanks in advance. Scott

    Thursday, December 20, 2012 9:06 AM

Answers

  • Hi Scott;

    The auto generated code is produced by a T4 templet which have a .tt file extension. These files can be modified to add the features that you need. Please have a look at the following article for info on the subject.

    T4 Templates and the Entity Framework

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by scottsanpedro Thursday, December 20, 2012 6:02 PM
    Thursday, December 20, 2012 4:19 PM

All replies

  • Hi Scott;

    The auto generated code is produced by a T4 templet which have a .tt file extension. These files can be modified to add the features that you need. Please have a look at the following article for info on the subject.

    T4 Templates and the Entity Framework

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by scottsanpedro Thursday, December 20, 2012 6:02 PM
    Thursday, December 20, 2012 4:19 PM
  • You don'r need to change the autogenerated file, only to change connection string.

    DbContext provides overloaded constructor where you can pass connection string corresponding to the database you need.That's all.

    Let me know if you need something else.


    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 4:25 PM
  • thanks to both. Just following up on bootstrap2.

    I currently set the connection in the xxx.context.cs file. Can you point me in the right direction to do this via the constructor?

    namespace HiggCast.Model
    {
        public partial class HiggidyPiesEntities : DbContext
        {
            public HiggidyPiesEntities()
                : base(GetConnection())
            {
            }
    
            private static string GetConnection()
            {
                switch (System.Environment.MachineName)
                {
                    case General.ComputerName:
                        return "name=HiggidyPiesEntities";
                    default:
                        if (NetworkCheck("192.168.16.3"))
                        {
                            return "name=HiggidyPiesEntitiesInt";
                        }
                        break;
                }
                return "name=HiggidyPiesEntities";
                //else
                //{
                //    return "name=AercoEntitiesExt";
                //}
            }
    
            public static bool NetworkCheck(string server)
            {
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(server);
                if (reply != null)
                    return reply.Status == IPStatus.Success;
                else
                    return false;
            }

    Thanks for you help on this. Scott

    Thursday, December 20, 2012 5:04 PM
  • Hi,

    I'm using this approach. I added a new file to extend the partial class, to add a constructor. Then when I create a new reference to the context, the connection string is used to point to the required environment.

    MyContext.cs
        public partial class MyDBContext : DbContext
        {
            public MyDBContext(string nameOrConnectionString)
                : base(nameOrConnectionString)
            {
            
            }
    
        }
    
    Use the context:
            private void InitializeContext()
            {
    ...
    
                _context = new MyContext.MyDBContext(connectionStringBuilder.ToString());
    
    }


    Ronald

    Friday, December 21, 2012 9:06 AM
  • yes that has worked. Makes life easier. Thanks for your input. All gratefully received.
    Friday, December 21, 2012 10:43 AM