locked
Pass SqlConnection and SqlTransaction to POCO DbContext RRS feed

  • Question

  • Hi,

    I have a DA project that has it's CRUD functions accepting a SqlConnection and SqlTransaction object (these connections are actually held in the BO layer, and passed to the DA methods in order to ensure that multiple calls across a range of objects stay in the same transaction).

    I'd like to refactor this DA layer to use POCO and EF, instead of ADO.Net (and calls to CRUD procedures). However, if I do, I have a requirement that I can simply replace the existing DLL with my new DLL (i.e. I cannot replace any code in the BO).

    As a result, I have written methods such as the below using the DbContext object:

    public override void DataSelect(SqlConnection conn, SqlTransaction trans) 
    { 
      using (DAContext ctxt = new DAContext(connection))
      {
        //Do Work
      }
    }

    Here is the DbContext object I have created:

        public class DAContext : DbContext
        {
            public DAContext()
                :base()
            {
                
            }
    
            public DAContext(DbConnection connection)
                : base(connection, false)
            {
                
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                //Stop Pluralising the Object names for table names.
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
                //Set any property ending in "Key" as a key type.
                modelBuilder.Properties().Where(prop => prop.Name.ToLower().EndsWith("key")).Configure(config => config.IsKey());
     
            }
    
            //Add DbSets
        }

    I *think* this is now using the SqlConnection object I have passed through, but I'm not sure how to make it use the Transaction object. Can anyone help?

    Thanks

    Fergal

    Tuesday, May 28, 2013 12:24 PM

Answers

  • Typical really, I think I've solved my own problem almost as soon as I posted this. I need this in the constructor:

    public DAContext(DbConnection connection, DbTransaction trans)
                : base(connection, false)
            {
                this.Database.UseTransaction(trans);
            }
    If I'm doing this wrong, please feel free to correct me though...

    Tuesday, May 28, 2013 12:51 PM

All replies

  • Typical really, I think I've solved my own problem almost as soon as I posted this. I need this in the constructor:

    public DAContext(DbConnection connection, DbTransaction trans)
                : base(connection, false)
            {
                this.Database.UseTransaction(trans);
            }
    If I'm doing this wrong, please feel free to correct me though...

    Tuesday, May 28, 2013 12:51 PM
  • Hi Obsidian,

    I'm glad to hear that you have solved this problem.

    I'm not able to find the UserTransaction() method in Database class. Could you please tell us how are you using this method?

    Thanks for your contribution to this forum.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 30, 2013 2:43 AM
  • Hi Chester,

    I'm using the 6.0.0-alpha3 version of Entity Framework on NuGet, which might be why you aren't seeing it. I upgraded to that when looking at other pages and finding that methods in the DbContext ModelBuilder object weren't available to me under EF 5.

    Thursday, May 30, 2013 7:22 AM
  • Hi Obsidian,

    Thanks for your information. It is helpful.

    Have a nice day!

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 30, 2013 8:28 AM