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


C# DbSet.Add方法代码示例

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


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

示例1: With_valid_entity_returns_the_added_entity

            public void With_valid_entity_returns_the_added_entity()
            {
                var set = new DbSet<FakeEntity>(new Mock<InternalSetForMock<FakeEntity>>().Object);
                var entity = new FakeEntity();

                var retVal = set.Add(entity);

                Assert.Same(entity, retVal);
            }
开发者ID:junxy,项目名称:entityframework,代码行数:9,代码来源:DbSetTests.cs

示例2: RetrieveAndPopulateExchanges

 public static IEnumerable<Exchange> RetrieveAndPopulateExchanges(IEnumerable<DateTime> datesToRetrieve, string targetCurrencyCode, DbSet<Exchange> exchangesDbSet)
 {
     var retrievedExchanges = Enumerable.Empty<Exchange>();
     foreach (var dateToProcess in datesToRetrieve)
     {
         IEnumerable<Exchange> exchangesOfDate;
         try
         {
             try
             {
                 exchangesOfDate = RequestDefaultExternalResource(dateToProcess, targetCurrencyCode);
             }
             catch (ExchangeResource.ResourceBrokageException rbEx)
             {
                 throw new ExchangeResource.NotifyResourceMaintainer("The resource is broken or violates protocol.", rbEx);
             }
             catch (ExchangeResource.BaseException)
             {
                 throw;
             }
             catch (Exception ex)
             {
                 var erEx = new ExchangeResource.NotifyResourceDeveloper(ex);
                 erEx.AddFailedDate(dateToProcess);
                 throw erEx;
             }
         }
         catch (ExchangeResource.BaseException resEx)
         {
             var triedDates = retrievedExchanges.Select(e => e.Date).Concat(resEx.GetFailedDates());
             var untouchedDates = datesToRetrieve.Except(triedDates);
             resEx.AddUntouchedDates(untouchedDates);
             throw;
         }
         foreach (var exchange in exchangesOfDate)
             exchangesDbSet.Add(exchange); // TODO: dangerous if given exchange is in the DB already. `SaveChanges` will fail (and drop other changes?).
         Trace.WriteLine("Database is populated with "+exchangesOfDate.Count()+" entries for date "+dateToProcess.ToString("yyyy-MM-dd")+".");
         retrievedExchanges = retrievedExchanges.Concat(exchangesOfDate);
         var targetExchanges = exchangesOfDate.Where(ex => ex.CurrencyCode == targetCurrencyCode);
         if (targetExchanges.Count() == 0)
         {
             var resEx = new ExchangeResource.TryRequestingOtherDatesOrCurrency(
                 String.Format(
                     "No rates for currency code {0} on {1}.",
                     targetCurrencyCode,
                     dateToProcess.ToString("yyyy-MM-dd")
                 )
             );
             resEx.AddFailedDate(dateToProcess);
             throw resEx;
         }
     }
     return retrievedExchanges;
 }
开发者ID:ilyaigpetrov,项目名称:test-task-csharp-aspnet-saritasa-2014,代码行数:54,代码来源:ExchangeExternalResources.cs

示例3: AddGroups1

        //var groups = scenario.ScenarioGroups;
        //var sc = scenario.Scenarios;
        //DateTime now = DateTime.UtcNow;
        //        try
        //        {
        //            AddGroups1(groups, now);
        //scenario.SaveChanges();
        //            AddGroups2(groups, now);
        //scenario.SaveChanges();
        //            //AddScenarios(sc, now);
        //        }
        //        catch (DbEntityValidationException dbEx)
        //        {
        //            foreach (var validationErrors in dbEx.EntityValidationErrors)
        //            {
        //                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", validationErrors.Entry.Entity.GetType().Name, validationErrors.Entry.State);
        //                foreach (var validationError in validationErrors.ValidationErrors)
        //                {
        //                    Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
        //                }
        //            }
        //        }
        //        Console.WriteLine("*****Scenario Group List*****");
        //        foreach (ScenarioGroup item in groups)
        //        {
        //            Console.WriteLine(item.ScenarioGroupID);
        //            foreach (ScenarioGroupDetail item2 in item.ScenarioGroupDetails)
        //            {
        //                Console.WriteLine(item2.ScenarioGroupDetailID + " " + item2.Name);
        //            }
        //        }
        static void AddGroups1(DbSet<ScenarioGroup> root, DateTime now)
        {
            ScenarioGroup sg = new ScenarioGroup();
            sg.StartTime = now.AddDays(-100);
            sg.EndTime = DateTime.MaxValue;
            sg.CreatedBy = "me";
            sg.CreatedAt = now;
            root.Add(sg);

            ScenarioGroupDetail sgd = new ScenarioGroupDetail();
            sgd.Name = "Test 2-1";
            sgd.StartTime = now.AddDays(-100);
            sgd.EndTime = now.AddDays(-50);
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now.AddDays(-50);
            //sgd.ScenarioGroup = sg;
            sg.ScenarioGroupDetails.Add(sgd);

            sgd = new ScenarioGroupDetail();
            sgd.Name = "Test 2-2";
            sgd.StartTime = now.AddDays(-50);
            sgd.EndTime = now.AddDays(-20);
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now.AddDays(-20);
            //sgd.ScenarioGroup = sg;
            sg.ScenarioGroupDetails.Add(sgd);

            sgd = new ScenarioGroupDetail();
            sgd.Name = "Test 2-3";
            sgd.StartTime = now.AddDays(-20);
            sgd.EndTime = DateTime.MaxValue;
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now.AddDays(-20);
            //sgd.ScenarioGroup = sg;
            sg.ScenarioGroupDetails.Add(sgd);
        }
开发者ID:Juliusz,项目名称:Test,代码行数:67,代码来源:Program.cs

示例4: Members_check_arguments

        public void Members_check_arguments()
        {
            Assert.Equal(
                "context",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(() => new DbSet<Random>(null)).ParamName);

            var set = new DbSet<Random>(new Mock<DbContext>().Object);

            Assert.Equal(
                "entity",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(() => set.Add(null)).ParamName);
            Assert.Equal(
                "entity",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.ThrowsAsync<ArgumentNullException>(() => set.AddAsync(null)).Result.ParamName);
            Assert.Equal(
                "entity",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(
                    () => set.AddAsync(null, new CancellationToken()).GetAwaiter().GetResult()).ParamName);

            Assert.Equal(
                "entity",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(() => set.Update(null)).ParamName);
            Assert.Equal(
                "entity",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.ThrowsAsync<ArgumentNullException>(() => set.UpdateAsync(null)).Result.ParamName);
            Assert.Equal(
                "entity",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.ThrowsAsync<ArgumentNullException>(() => set.UpdateAsync(null, new CancellationToken())).Result.ParamName);
        }
开发者ID:charlyraffellini,项目名称:EntityFramework,代码行数:36,代码来源:DbSetTest.cs

示例5: AddGroups2

        static void AddGroups2(DbSet<ScenarioGroup> root, DateTime now)
        {
            ScenarioGroup sg1 = root.Find(2);

            ScenarioGroup sg = new ScenarioGroup();
            sg.StartTime = now.AddDays(-80);
            sg.EndTime = DateTime.MaxValue;
            sg.CreatedBy = "me";
            sg.CreatedAt = now;
            root.Add(sg);

            ScenarioGroupDetail sgd = new ScenarioGroupDetail();
            sgd.Name = "Test-Child-2";
            sgd.StartTime = now.AddDays(-80);
            sgd.EndTime = now.AddDays(-40);
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now.AddDays(-40);
            sg.ScenarioGroupDetails.Add(sgd);

            sgd = new ScenarioGroupDetail();
            sgd.Name = "Test-Child-2";
            sgd.StartTime = now.AddDays(-40);
            sgd.EndTime = now.AddDays(-20);
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now.AddDays(-20);
            sg.ScenarioGroupDetails.Add(sgd);

            sgd = new ScenarioGroupDetail();
            sgd.Name = "Test-Child-2";
            sgd.StartTime = now.AddDays(-20);
            sgd.EndTime = DateTime.MaxValue;
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now.AddDays(-20);
            sg.ScenarioGroupDetails.Add(sgd);
        }
开发者ID:Juliusz,项目名称:Test,代码行数:35,代码来源:Program.cs

示例6: AddScenarios

        //static void AddGroups3(DbSet<ScenarioGroup> root, DateTime now)
        //{
        //    ScenarioGroup sg = new ScenarioGroup();
        //    sg.Name = "Test-Child2";
        //    sg.ScenarioGroupID = 2;
        //    sg.ScenarioGroupParentID = 1;
        //    sg.StartTime = now.AddDays(-80);
        //    sg.EndTime = DateTime.MaxValue;
        //    sg.CreatedBy = "me";
        //    sg.TimeStamp = now.AddDays(-80);
        //    root.Add(sg);
        //}
        static void AddScenarios(DbSet<ScenarioGroup> setScenarioGroup, DbSet<Scenario> setScenario, DateTime now)
        {
            ScenarioGroup sg = new ScenarioGroup();
            sg.StartTime = now.AddDays(-80);
            sg.EndTime = DateTime.MaxValue;
            sg.CreatedBy = "me";
            sg.CreatedAt = now;
            setScenarioGroup.Add(sg);

            ScenarioGroupDetail sgd = new ScenarioGroupDetail();
            sgd.Name = "Test-Child-2";
            sgd.StartTime = now.AddDays(-80);
            sgd.EndTime = DateTime.MaxValue;
            sgd.CreatedBy = "me";
            sgd.CreatedAt = now;
            sg.ScenarioGroupDetails.Add(sgd);

            Scenario s = new Scenario();
            s.StartTime = now.AddDays(-80);
            s.EndTime = DateTime.MaxValue;
            s.CreatedBy = "me";
            s.CreatedAt = now;
            setScenario.Add(s);

            ScenarioDetail sd = new ScenarioDetail();
            sd.Name = "Scenario A";
            sd.StartTime = now.AddDays(-80);
            sd.EndTime = DateTime.MaxValue;
            sd.CreatedBy = "me";
            sd.CreatedAt = now;
            s.ScenarioDetails.Add(sd);

            Scenario s2 = new Scenario();
            s.StartTime = now.AddDays(-80);
            s.EndTime = DateTime.MaxValue;
            s.CreatedBy = "me";
            s.CreatedAt = now;
            setScenario.Add(s2);

            ScenarioDetail sd2 = new ScenarioDetail();
            sd.Name = "Derive from A";
            sd.StartTime = now.AddDays(-80);
            sd.EndTime = DateTime.MaxValue;
            sd.CreatedBy = "me";
            sd.CreatedAt = now;
            s2.ScenarioDetails.Add(sd2);

            s.DerivedScenarios.Add(s2);
        }
开发者ID:Juliusz,项目名称:Test,代码行数:61,代码来源:Program.cs

示例7: CreateCodeTemplateIfNeeded

        private void CreateCodeTemplateIfNeeded(IDataService dataService, DbSet<SigCodeTemplate> codeTemplates, string codeTemplateValue, string codeTemplateDisplayeName)
        {
            if ((from c in codeTemplates where c.Code.Equals(codeTemplateValue) select c).Any() == false)
            {
                SigCodeTemplate codeNodeTemplate = new SigCodeTemplate();
                codeNodeTemplate.Code = codeTemplateValue;
                codeNodeTemplate.Libelle = codeTemplateDisplayeName;
                codeTemplates.Add(codeNodeTemplate);
                dataService.DataContext.SaveChanges();

            }
        }
开发者ID:ChampsyGnom,项目名称:GeoPat,代码行数:12,代码来源:CartoService.cs

示例8: CreateCodeLayerIfNeeded

 private void CreateCodeLayerIfNeeded(IDataService dataService, DbSet<SigCodeLayer> codeLayers, string codeLayerValue)
 {
     if ((from c in codeLayers where c.Code.Equals(codeLayerValue) select c).Any() == false)
     {
         SigCodeLayer codeLayerGoogle = new SigCodeLayer();
         codeLayerGoogle.Code = codeLayerValue;
         codeLayerGoogle.Libelle = codeLayerValue;
         codeLayers.Add(codeLayerGoogle);
         dataService.DataContext.SaveChanges();
     }
 }
开发者ID:ChampsyGnom,项目名称:GeoPat,代码行数:11,代码来源:CartoService.cs


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