public static void Execute()
        {
            // Create BD
            using (var context = new EntityContext())
            {
                My.CreateBD(context);
            }

            // CLEAN
            using (var context = new EntityContext())
            {
                context.EntitySimples.RemoveRange(context.EntitySimples);

                context.SaveChanges();
            }

            // SEED
            using (var context = new EntityContext())
            {
                for (int i = 0; i < 3; i++)
                {
                    context.EntitySimples.Add(new EntitySimple {
                        ColumnInt = i
                    });
                }

                context.SaveChanges();
            }



            // TEST
            using (var context = new EntityContext())
            {
                var connection = context.Database.GetDbConnection();
                connection.Open();
                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"   
if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple')
BEGIN
DROP PROCEDURE [dbo].[PROC_Get_EntitySimple]
END 
						"                        ;
                    commande.ExecuteNonQuery();
                }

                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"    
CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple]

	@ParameterID INT    ,
	@ParameterInt INT = NULL OUTPUT 


AS
BEGIN 
update EntitySimples
Set ColumnInt = @ParameterID ;

Set @ParameterInt = @ParameterID +1 
END
						"                        ;
                    commande.ExecuteNonQuery();
                }

                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"    
CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple2]

	@ParameterID INT    ,
	@ParameterInt INT = NULL OUTPUT 


AS
BEGIN 
update EntitySimples
Set ColumnInt = @ParameterID ;

Set @ParameterInt = @ParameterID +1 

select * from EntitySimples
END
						"                        ;
                    commande.ExecuteNonQuery();
                }
            }

            // TEST
            using (var context = new EntityContext())
            {
                var proc_Get_EntitySimple = new Proc_Get_EntitySimple()
                {
                    ParameterID = 2
                };

                using (var tran = new TransactionScope())
                {
                    context.Database.ExecuteStoredProcedure(proc_Get_EntitySimple);
                }

                var transaction = context.Database.BeginTransaction();

                var list2 = context.Database.ExecuteStoredProcedure <EntitySimple>(proc_Get_EntitySimple);

                context.Database.ExecuteStoredProcedure(proc_Get_EntitySimple);

                transaction.Rollback();

                var list = context.EntitySimples.ToList();
            }

            // TEST
            using (var context = new EntityContext())
            {
                var proc_Get_EntitySimple = new Proc_Get_EntitySimple2()
                {
                    ParameterID = 2
                };
                var transaction = context.Database.BeginTransaction();
                context.Database.ExecuteStoredProcedure <EntitySimple>(proc_Get_EntitySimple);


                transaction.Rollback();
                var lisa = context.EntitySimples.ToList();


                var list = context.EntitySimples.ToList();
            }

            using (var context = new EntityContext())
            {
                var proc_Get_EntitySimple = new Proc_Get_EntitySimple2()
                {
                    ParameterID = 2
                };


                using (var tran = new TransactionScope())
                {
                    context.Database.ExecuteStoredProcedure <EntitySimple>(proc_Get_EntitySimple);
                }
                var list = context.EntitySimples.ToList();
            }

            using (var context = new EntityContext())
            {
                var proc_Get_EntitySimple = new Proc_Get_EntitySimple2()
                {
                    ParameterID = 2
                };
                using (var tran = new TransactionScope())
                {
                    context.Database.ExecuteStoredProcedure <EntitySimple>(proc_Get_EntitySimple);
                    tran.Complete();
                }

                var list = context.EntitySimples.ToList();
            }
        }
        public static void Execute()
        {
            // Create BD
            using (var context = new EntityContext())
            {
                My.CreateBD(context);
            }

            // CLEAN
            using (var context = new EntityContext())
            {
                context.EntitySimples.RemoveRange(context.EntitySimples);

                context.SaveChanges();
            }

            // SEED
            using (var context = new EntityContext())
            {
                for (int i = 0; i < 3; i++)
                {
                    context.EntitySimples.Add(new EntitySimple {
                        ColumnInt = i
                    });
                }

                context.SaveChanges();
            }



            // TEST
            using (var context = new EntityContext())
            {
                var connection = context.Database.GetDbConnection();
                connection.Open();
                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"   
if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple')
BEGIN
DROP PROCEDURE [dbo].[PROC_Get_EntitySimple]
END 
						"                        ;
                    commande.ExecuteNonQuery();
                }

                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"    
CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple]

	@ParameterID INT    ,
	@ParameterInt INT = NULL OUTPUT 


AS
BEGIN 

WAITFOR DELAY '00:00:05'
Select * from EntitySimples
Where ColumnInt = @ParameterID
Set @ParameterInt = @ParameterID +1 
END
						"                        ;
                    commande.ExecuteNonQuery();
                }
            }

            // TEST
            using (var context = new EntityContext())
            {
                var cts = new CancellationTokenSource();

                var proc_Get_EntitySimple = new Proc_Get_EntitySimple()
                {
                    ParameterID = 2
                };
                var entity = context.Database.ExecuteStoredProcedureAsync <EntitySimple>(proc_Get_EntitySimple, cts.Token);
                //  cts.Cancel();
                entity.Wait();

                var proc_Get_EntitySimple2 = new Proc_Get_EntitySimple()
                {
                    ParameterID = 2
                };
                var entaity = context.Database.ExecuteStoredProcedureFirstOrDefaultAsync <EntitySimple>(proc_Get_EntitySimple2);               //, cts.Token).Result;
                entaity.Wait();
            }


            using (var context = new EntityContext())
            {
                var cts = new CancellationTokenSource();
                var proc_Get_EntitySimple = new Proc_Get_EntitySimple()
                {
                    ParameterID = 2
                };
                var task = context.Database.ExecuteStoredProcedureAsync(proc_Get_EntitySimple, cts.Token);
                //   cts.Cancel();
                task.Wait();
            }
        }
示例#3
0
        public static void Execute()
        {
            // Create BD
            using (var context = new EntityContext())
            {
                My.CreateBD(context);
            }

            // CLEAN
            using (var context = new EntityContext())
            {
                context.EntitySimples.RemoveRange(context.EntitySimples);

                context.SaveChanges();
            }

            // SEED
            using (var context = new EntityContext())
            {
                for (int i = 0; i < 3; i++)
                {
                    context.EntitySimples.Add(new EntitySimple {
                        ColumnInt = i
                    });
                }

                context.SaveChanges();
            }



            // TEST
            using (var context = new EntityContext())
            {
                var connection = context.Database.GetDbConnection();
                connection.Open();
                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"   
if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple')
BEGIN
DROP PROCEDURE [dbo].[PROC_Get_EntitySimple]
END 
						"                        ;
                    commande.ExecuteNonQuery();
                }

                using (var commande = connection.CreateCommand())
                {
                    commande.CommandText = @"    
CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple]

	@ParameterID INT    ,
	@ParameterInt INT = NULL OUTPUT 


AS
BEGIN 
WAITFOR DELAY '00:00:08';
select * from  EntitySimples ;
END
						"                        ;
                    commande.ExecuteNonQuery();
                }
            }

            // TEST
            using (var ctx = new EntityContext())
            {
                var proc_Get_EntitySimple = new Proc_Get_EntitySimple()
                {
                    ParameterID = 2
                };
                ctx.Database.SetCommandTimeout(5);



                var t = ctx.Database.ExecuteStoredProcedure <EntitySimple>(proc_Get_EntitySimple);
                ctx.Database.ExecuteStoredProcedure(proc_Get_EntitySimple);
                //	var list = context.EntitySimples.ToList();
            }
        }