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


C# DatabaseContext.Set方法代码示例

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


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

示例1: CreateArrangementWithVersions

        public void CreateArrangementWithVersions()
        {
            using (DatabaseContext ctx = new DatabaseContext())
            {
                ArrangementRepository repository = new ArrangementRepository(ctx);
                var arrangement = repository.Add();

                arrangement.Name = "Koops Furness";
                arrangement.BpNumber = "123456789";

                var version1 = new ArrangementVersion {Version = 1};
                var version2 = new ArrangementVersion {Version = 2};

                arrangement.Versions.Add(version1);
                arrangement.Versions.Add(version2);

                Assert.IsTrue(arrangement.Id == 0);
                Assert.IsTrue(version1.Id == 0);
                Assert.IsTrue(version2.Id == 0);

                ctx.SaveChanges();
                Assert.IsTrue(arrangement.Id > 0);
                Assert.IsTrue(version1.Id > 0);
                Assert.IsTrue(version2.Id > 0);
                Assert.AreEqual(arrangement, version1.Arrangement);
                Assert.AreEqual(arrangement, version2.Arrangement);
            }

            using (DatabaseContext ctx = new DatabaseContext())
            {
                var arrangement = ctx.Set<Arrangement>().Single();
                Assert.AreEqual("Koops Furness", arrangement.Name);
                Assert.AreEqual("123456789", arrangement.BpNumber);

                Assert.IsEmpty(arrangement.Users);
                Assert.IsNotEmpty(arrangement.Versions);
                Assert.AreEqual(2, arrangement.Versions.Count);
                Assert.AreEqual(arrangement, arrangement.Versions.First().Arrangement);
                Assert.AreEqual(arrangement, arrangement.Versions.Last().Arrangement);
            }
        }
开发者ID:phoogestijn,项目名称:EntityFrameworkSandbox,代码行数:41,代码来源:ArrangementTests.cs

示例2: CreateEmptyArrangement

        public void CreateEmptyArrangement()
        {
            using (DatabaseContext ctx = new DatabaseContext())
            {
                ArrangementRepository repository = new ArrangementRepository(ctx);
                var arrangement = repository.Add();

                Assert.IsTrue(arrangement.Id == 0);
                arrangement.Name = "Koops Furness";
                arrangement.BpNumber = "123456789";

                ctx.SaveChanges();
                Assert.IsTrue(arrangement.Id > 0);
            }

            using (DatabaseContext ctx = new DatabaseContext())
            {
                var arrangement = ctx.Set<Model.Arrangement>().Single();
                Assert.AreEqual("Koops Furness", arrangement.Name);
                Assert.AreEqual("123456789", arrangement.BpNumber);

                Assert.IsEmpty(arrangement.Users);
                Assert.IsEmpty(arrangement.Versions);
            }
        }
开发者ID:phoogestijn,项目名称:EntityFrameworkSandbox,代码行数:25,代码来源:ArrangementTests.cs

示例3: Sandbox

        public void Sandbox()
        {
            using (DatabaseContext ctx = new DatabaseContext())
            {
                ArrangementRepository repository = new ArrangementRepository(ctx);
                var arrangement = repository.Add();
                arrangement.Name = "Koops Furness";
                arrangement.BpNumber = "123456789";

                var arrangement2 = repository.Add();
                arrangement2.Name = "H4";
                arrangement2.BpNumber = "987654321";

                var arrangementVersion1 = new ArrangementVersion();
                arrangementVersion1.Version = 1;
                arrangementVersion1.Status = VersionStatus.Editing;

                var arrangementVersion2 = new ArrangementVersion();
                arrangementVersion2.Version = 2;
                arrangementVersion2.Status = VersionStatus.Archived;

                arrangement.Versions.Add(arrangementVersion1);
                arrangement.Versions.Add(arrangementVersion2);

                ctx.SaveChanges();
            }

            using (DatabaseContext ctx = new DatabaseContext())
            {
                var arrangement = ctx.Set<Arrangement>().First();
                Assert.AreEqual(2, arrangement.Versions.Count());
                var av = ctx.ObjectContext.CreateObject<ArrangementVersion>();
                av.Version = 33;
                av.Status = VersionStatus.Published;

                arrangement.Versions.Add(av);

                ctx.SaveChanges();
                av.Version = 333;
                ctx.SaveChanges();
            }

            using (DatabaseContext ctx = new DatabaseContext())
            {
                int aantalversies = ctx.Set<Arrangement>().First().Versions.Count();
                Assert.AreEqual(3, aantalversies);
            }

            using (DatabaseContext ctx = new DatabaseContext())
            {
                var arrangement = ctx.Set<Arrangement>().First(a => a.Id == 1);
                var arrangement2 = ctx.Set<Arrangement>().First(a => a.Id == 2);

                var versie = arrangement.Versions.First(av => av.Version == 1);

                Assert.AreEqual(3, arrangement.Versions.Count);

                versie.Arrangement = arrangement2;

                Assert.IsFalse(arrangement.Versions.Any(v => v.Id == versie.Id));
                Assert.IsTrue(arrangement2.Versions.Any(v => v.Id == versie.Id));

                //arrangement.Versions.Remove(versie);
                //arrangement2.Versions.Add(versie);

                ctx.SaveChanges();
            }

            using (DatabaseContext ctx = new DatabaseContext())
            {

                Arrangement arrangement = ctx.Set<Arrangement>().First(a => a.Id == 1);

                var entry = ctx.Entry<Arrangement>(arrangement);
                Assert.AreEqual(EntityState.Unchanged, entry.State);
                var temp1 = ctx.ChangeTracker.Entries().Single();

                arrangement.Name = "Test 1234";

                Assert.AreEqual(EntityState.Modified, entry.State);
                var temp2 = ctx.ChangeTracker.Entries();

                ctx.SaveChanges();
                Assert.AreEqual(EntityState.Unchanged, entry.State);
            }

            //using (DatabaseContext ctx = new DatabaseContext())
            //{
            //    var arrangement = ctx.Set<Arrangement>().First(a => a.Id == 2);
            //    ctx.Arrangements.Remove(arrangement);

            //    ctx.SaveChanges();
            //}
        }
开发者ID:phoogestijn,项目名称:EntityFrameworkSandbox,代码行数:94,代码来源:SandboxEnv.cs


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