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


C# Session.Insert方法代码示例

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


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

示例1: GetSession

        private ISession GetSession() {
            var engine = new InMemoryEngine() { Configuration = new TestConfiguration() };
            var session = new Session(engine, new Mock<ISessionState>().Object);
            
            var authors = new List<User> { new User { Username = "Bob" }, new User { Username = "Mark" }, new User { Username = "James" } };
            session.Insert(authors);
            
            var blogs = new List<Blog> { new Blog { Owner = authors[0], Title = "Bob's Blog" } };
            session.Insert(blogs);

            var posts = new List<Post> {
                                           new Post { Author = authors[0], Blog = blogs[0], Title = "My First Post" },
                                           new Post { Author = authors[0], Blog = blogs[0], Title = "My Second Post" }
                                       };
            session.Insert(posts);

            var comments = new List<Comment> {
                                                 new Comment { User = authors[1], Post = posts[0], Content = "This is marks comment on the first post" },
                                                 new Comment {
                                                                 User = authors[1],
                                                                 Post = posts[1],
                                                                 Content = "This is marks comment on the second post"
                                                             },
                                                 new Comment {
                                                                 User = authors[2],
                                                                 Post = posts[0],
                                                                 Content = "This is james' comment on the first post"
                                                             },
                                                 new Comment {
                                                                 User = authors[2],
                                                                 Post = posts[1],
                                                                 Content = "This is james' comment on the second post"
                                                             },
                                                 new Comment { User = authors[0], Post = posts[0], Content = "This is bob's comment on the first post" },
                                                 new Comment {
                                                                 User = authors[0],
                                                                 Post = posts[1],
                                                                 Content = "This is bob's comment on the second post"
                                                             },
                                             };
            session.Insert(comments);
            return session;
        }
开发者ID:gitter-badger,项目名称:dashing,代码行数:43,代码来源:InMemoryEngineTests.cs

示例2: InsertThrowsObjectDisposedExceptionIfDisposed

        public void InsertThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new Session(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            using (session)
            {
            }

            Assert.Throws<ObjectDisposedException>(() => session.Insert(new Customer()));
        }
开发者ID:natarajanmca11,项目名称:MicroLite,代码行数:16,代码来源:SessionTests.cs

示例3: InsertThrowsArgumentNullExceptionForNullInstance

        public void InsertThrowsArgumentNullExceptionForNullInstance()
        {
            var session = new Session(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<ArgumentNullException>(() => session.Insert(null));

            Assert.Equal("instance", exception.ParamName);
        }
开发者ID:natarajanmca11,项目名称:MicroLite,代码行数:14,代码来源:SessionTests.cs

示例4: InsertThrowsMicroLiteExceptionIfExecuteScalarThrowsException

        public void InsertThrowsMicroLiteExceptionIfExecuteScalarThrowsException()
        {
            var customer = new Customer();

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildInsertSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(new SqlQuery(""));
            mockSqlDialect.Setup(x => x.BuildSelectInsertIdSqlQuery(It.IsNotNull<IObjectInfo>())).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteScalar()).Throws<InvalidOperationException>();

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object);

            var session = new Session(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<MicroLiteException>(() => session.Insert(customer));

            Assert.NotNull(exception.InnerException);
            Assert.Equal(exception.InnerException.Message, exception.Message);

            // Command should still be disposed.
            mockCommand.VerifyAll();
        }
开发者ID:natarajanmca11,项目名称:MicroLite,代码行数:33,代码来源:SessionTests.cs

示例5: InsertInvokesListeners

        public void InsertInvokesListeners()
        {
            var customer = new Customer();
            object identifier = 23543;

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildInsertSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(new SqlQuery(""));
            mockSqlDialect.Setup(x => x.BuildSelectInsertIdSqlQuery(It.IsNotNull<IObjectInfo>())).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteScalar()).Returns(identifier);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object);

            int counter = 0;

            var mockListener1 = new Mock<IInsertListener>();
            mockListener1.Setup(x => x.AfterInsert(customer, identifier)).Callback(() => Assert.Equal(4, ++counter));
            mockListener1.Setup(x => x.BeforeInsert(customer)).Callback(() => Assert.Equal(1, ++counter));

            var mockListener2 = new Mock<IInsertListener>();
            mockListener2.Setup(x => x.AfterInsert(customer, identifier)).Callback(() => Assert.Equal(3, ++counter));
            mockListener2.Setup(x => x.BeforeInsert(customer)).Callback(() => Assert.Equal(2, ++counter));

            var session = new Session(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new[] { mockListener1.Object, mockListener2.Object },
                new IUpdateListener[0]);

            session.Insert(customer);

            mockListener1.VerifyAll();
            mockListener2.VerifyAll();
        }
开发者ID:natarajanmca11,项目名称:MicroLite,代码行数:41,代码来源:SessionTests.cs

示例6: ConventionMappingConvention

        public void InsertBuildsAndExecutesIndividualCommandsIfIdentifierStrategyNotAssignedAndSqlDialectSupportsSelectInsertedIdentifierAndDbDriverDoesNotSupportBatchedQueries()
        {
            ObjectInfo.MappingConvention = new ConventionMappingConvention(
                UnitTest.GetConventionMappingSettings(IdentifierStrategy.DbGenerated));

            var customer = new Customer();
            var insertSqlQuery = new SqlQuery("INSERT");
            var selectIdSqlQuery = new SqlQuery("SELECT");
            object identifier = 23543;

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildInsertSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(insertSqlQuery);
            mockSqlDialect.Setup(x => x.BuildSelectInsertIdSqlQuery(It.IsNotNull<IObjectInfo>())).Returns(selectIdSqlQuery);
            mockSqlDialect.Setup(x => x.SupportsSelectInsertedIdentifier).Returns(true);

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery());
            mockCommand.Setup(x => x.ExecuteScalar()).Returns(identifier);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object);
            mockDbDriver.Setup(x => x.SupportsBatchedQueries).Returns(false);

            var session = new Session(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            session.Insert(customer);

            mockSqlDialect.VerifyAll();
            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
开发者ID:natarajanmca11,项目名称:MicroLite,代码行数:40,代码来源:SessionTests.cs

示例7: InsertBuildsAndExecutesAnInsertCommandOnlyIfIdentifierStrategyAssigned

        public void InsertBuildsAndExecutesAnInsertCommandOnlyIfIdentifierStrategyAssigned()
        {
            ObjectInfo.MappingConvention = new ConventionMappingConvention(
                UnitTest.GetConventionMappingSettings(IdentifierStrategy.Assigned));

            var customer = new Customer
            {
                Id = 12345
            };

            var insertSqlQuery = new SqlQuery("INSERT");

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildInsertSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(insertSqlQuery);
            mockSqlDialect.Setup(x => x.SupportsSelectInsertedIdentifier).Returns(false);

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object);

            var session = new Session(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            session.Insert(customer);

            mockSqlDialect.Verify(x => x.BuildInsertSqlQuery(It.IsNotNull<IObjectInfo>(), customer), Times.Once());
            mockSqlDialect.Verify(x => x.BuildSelectInsertIdSqlQuery(It.IsAny<IObjectInfo>()), Times.Never());

            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
开发者ID:natarajanmca11,项目名称:MicroLite,代码行数:41,代码来源:SessionTests.cs

示例8: ConventionMappingConvention

        public void InsertBuildsAndExecutesIndividualQueriesIfIdentifierStrategyDbGeneratedSqlDialectSupportsIdentityTheDbDriverDoesNotSupportBatchedQueries()
        {
            ObjectInfo.MappingConvention = new ConventionMappingConvention(
                UnitTest.GetConventionMappingSettings(IdentifierStrategy.DbGenerated));

            var customer = new Customer();
            var insertSqlQuery = new SqlQuery("INSERT");
            var selectIdSqlQuery = new SqlQuery("SELECT");
            object identifier = 23543;

            var mockInsertCommand = new Mock<IDbCommand>();
            mockInsertCommand.Setup(x => x.ExecuteNonQuery()).Returns(1);
            mockInsertCommand.As<IDisposable>().Setup(x => x.Dispose());

            var mockSelectCommand = new Mock<IDbCommand>();
            mockSelectCommand.Setup(x => x.ExecuteScalar()).Returns(identifier);
            mockInsertCommand.As<IDisposable>().Setup(x => x.Dispose());

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildInsertSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(insertSqlQuery);
            mockSqlDialect.Setup(x => x.BuildSelectInsertIdSqlQuery(It.IsNotNull<IObjectInfo>())).Returns(selectIdSqlQuery);
            mockSqlDialect.Setup(x => x.SupportsIdentity).Returns(true);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.GetConnection(ConnectionScope.PerTransaction)).Returns(new Mock<IDbConnection>().Object);
            mockDbDriver.Setup(x => x.SupportsBatchedQueries).Returns(false);
            mockDbDriver.Setup(x => x.BuildCommand(insertSqlQuery)).Returns(mockInsertCommand.Object);
            mockDbDriver.Setup(x => x.BuildCommand(selectIdSqlQuery)).Returns(mockSelectCommand.Object);

            var session = new Session(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IListener[0]);

            session.Insert(customer);

            mockSqlDialect.VerifyAll();
            mockDbDriver.VerifyAll();
            mockInsertCommand.VerifyAll();
            mockSelectCommand.VerifyAll();
        }
开发者ID:rubenalves,项目名称:MicroLite,代码行数:42,代码来源:SessionTests.cs


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