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


C# DbContext.GetRequiredService方法代码示例

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


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

示例1: Compare_returns_0_only_for_commands_that_are_equal

        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model = new Entity.Metadata.Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new DbContextOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryDatabase();

            var contextServices = new DbContext(optionsBuilder.Options).GetService();
            var stateManager = contextServices.GetRequiredService<IStateManager>();

            var key = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());
            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider());
            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());
            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider());
            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());
            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider());
            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider()), null));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.TestProvider()),
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
开发者ID:491134648,项目名称:EntityFramework,代码行数:74,代码来源:ModificationCommandComparerTest.cs


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