当前位置: 首页>>代码示例>>C#>>正文


C# DataContext.All方法代码示例

本文整理汇总了C#中DataContext.All方法的典型用法代码示例。如果您正苦于以下问题:C# DataContext.All方法的具体用法?C# DataContext.All怎么用?C# DataContext.All使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataContext的用法示例。


在下文中一共展示了DataContext.All方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Execute

        public void Execute()
        {
            var manufacturerId = 0;

            BeginTest.To(new DataContext())
                .Should("Get datacontext transaction mode").Assert(d => d.TransactionMode == Transaction.Begin)
                .Should("Get a value from database").Assert(d =>
                {
                    manufacturerId = d.GetValue<int>("SELECT TOP 1 TheId FROM Le_Manufacturer");
                    return manufacturerId > 0;
                })
                .Should("Insert car with a transactional datacontext").Assert(d =>
                {
                    var car = new Car { Name = "Esprit", ModelYear = 1976, ManufacturerId = manufacturerId };
                    var id = d.Execute("INSERT INTO Car VALUES (@Name, @ModelYear, @Date, @Chassis, @Mileage, @ManufacturerId)", car);
                    return id > 0;
                })
                .Should("Select new car without commit datacontext").Assert(d =>
                {
                    var car = d.First<Car>("SELECT * FROM Car WHERE Mileage IS NULL");
                    return car != null;
                })
                .Should("Commit datacontext transaction").Assert(d =>
                {
                    d.Commit();
                    return true;
                })
                .Should("Select commited data on database with another datacontext").Assert(d =>
                {
                    using (var dataContext = new DataContext())
                    {
                        var cars = dataContext.All<Car>("SELECT * FROM Car");
                        return cars.Count == 1;
                    }
                })
                .Should("Get values from database").Assert(d =>
                {
                    var mileages = d.GetValues<double?>("SELECT Mileage FROM Car");
                    return mileages.Count == 1 && mileages[0] == null;
                })
                .Should("Throw error when property format exception").Assert(d =>
                {
                    try
                    {
                        var car = d.First<ErrorCar>("SELECT * FROM Car");
                    }
                    catch (FormatException) { return true; }

                    return false;
                })
                .Should("Dispose datacontext close connection and transaction").Assert(d =>
                {
                    d.Dispose();
                    return d.Provider.DbConnection.State == ConnectionState.Closed;
                });
        }
开发者ID:plamikcho,项目名称:Thunderstruck,代码行数:56,代码来源:DataContextTest.cs

示例2: K_DataContext_Reader_All_Should_Return_An_Empty_Object_List_When_Query_Do_Not_Found_Items

        public void K_DataContext_Reader_All_Should_Return_An_Empty_Object_List_When_Query_Do_Not_Found_Items()
        {
            using (var context = new DataContext())
            {
                var cars = context.All<Car>("SELECT * FROM Car WHERE Name = 'Fusca'");

                cars.Should().BeEmpty();
            }
        }
开发者ID:bernardobrezende,项目名称:Thunderstruck,代码行数:9,代码来源:SqlTest.cs

示例3: D_DataContext_Transitional_Should_Not_Commit_Data_If_Not_Explicit_Commit_Command_Call

        public void D_DataContext_Transitional_Should_Not_Commit_Data_If_Not_Explicit_Commit_Command_Call()
        {
            using (var context = new DataContext())
            {
                var insertParameters = new { Name = "Bentley", Year = 1919 };
                context.Execute("INSERT INTO Le_Manufacturer VALUES (@Name, @Year)", insertParameters);
            }

            using (var context = new DataContext())
            {
                var manufacturers = context.All<Manufacturer>("SELECT * FROM Le_Manufacturer");

                manufacturers.Count().Should().Be(3);
                manufacturers.Should().Contain(m => m.Name == "Lotus");
                manufacturers.Should().Contain(m => m.Name == "General Motors");
            }
        }
开发者ID:bernardobrezende,项目名称:Thunderstruck,代码行数:17,代码来源:SqlTest.cs

示例4: G_DataContext_Should_Execute_A_Procedure

        public void G_DataContext_Should_Execute_A_Procedure()
        {
            using (var context = new DataContext())
            {
                var procedureParams = new { Status = "active" };
                var whoResults = context.All<WhoResult>("EXEC sp_who @Status", procedureParams);

                whoResults.Should().Contain(r => r.dbname == "ThunderTest" && r.cmd == "SELECT");
            }
        }
开发者ID:bernardobrezende,项目名称:Thunderstruck,代码行数:10,代码来源:SqlTest.cs

示例5: DataContext_Should_Get_A_List_Of_Objects_From_Database

        public void DataContext_Should_Get_A_List_Of_Objects_From_Database()
        {
            using (var context = new DataContext())
            {
                var cars = context.All<object>("SELECT * FROM Airplane");
            }

            connectionMock.Verify(c => c.Open(), Times.Once());
            connectionMock.Verify(c => c.Close(), Times.Once());
            connectionMock.Verify(c => c.BeginTransaction(), Times.Never());
            commandMock.Verify(c => c.ExecuteReader(), Times.Once());
            commandMock.VerifySet(c => c.CommandText = "SELECT * FROM Airplane");
            providerMock.Object.DbTransaction.Should().BeNull();
            dataReaderMock.Verify(r => r.Read(), Times.Once());
        }
开发者ID:bernardobrezende,项目名称:Thunderstruck,代码行数:15,代码来源:DataContextTest.cs


注:本文中的DataContext.All方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。