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


C# DbContext.Set方法代码示例

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


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

示例1: PatronService

 public PatronService()
 {
     _repository = new invoicesEntities();
     _patrons = _repository.Set<Patron>();
     _users = _repository.Set<User>();
     _recentlyViewedPatrons = _repository.Set<RecentlyViewedPatron>();
 }
开发者ID:txsll,项目名称:SLLInvoices,代码行数:7,代码来源:PatronService.cs

示例2: Cleanup

        public virtual void Cleanup(DbContext context)
        {
            context.Set<BuiltInNonNullableDataTypes>().RemoveRange(context.Set<BuiltInNonNullableDataTypes>());
            context.Set<BuiltInNullableDataTypes>().RemoveRange(context.Set<BuiltInNullableDataTypes>());

            context.SaveChanges();
        }
开发者ID:aishaloshik,项目名称:EntityFramework,代码行数:7,代码来源:BuiltInDataTypesFixtureBase.cs

示例3: CheckRegionAllowed

        public static void CheckRegionAllowed(IPrincipal principal,DbContext db, string regionID)
        {
            String userID = ((KawalDesaIdentity)principal.Identity).User.Id;
            if (userID == null)
                throw new ApplicationException("region is not allowed for thee");

            var region = db.Set<Region>()
                .AsNoTracking()
                .Include(r => r.Parent)
                .Include(r => r.Parent.Parent)
                .Include(r => r.Parent.Parent.Parent)
                .Include(r => r.Parent.Parent.Parent.Parent)
                .First(r => r.Id == regionID);

            var regionIDs = new List<string>();
            var current = region;
            while(current != null)
            {
                regionIDs.Add(current.Id);
                current = current.Parent;
            }

            var allowed = db.Set<UserScope>()
                .Any(s => s.fkUserId == userID && regionIDs.Contains(s.fkRegionId));
            if (!allowed)
                throw new ApplicationException("region is not allowed for thee");
        }
开发者ID:ekospinach,项目名称:kawaldesa,代码行数:27,代码来源:KawalDesaController.cs

示例4: AddTestData

        protected static void AddTestData(DbContext context)
        {
            var address1 = new Address { Street = "3 Dragons Way", City = "Meereen" };
            var address2 = new Address { Street = "42 Castle Black", City = "The Wall" };
            var address3 = new Address { Street = "House of Black and White", City = "Braavos" };

            context.Set<Person>().AddRange(
                new Person { Name = "Daenerys Targaryen", Address = address1 },
                new Person { Name = "John Snow", Address = address2 },
                new Person { Name = "Arya Stark", Address = address3 },
                new Person { Name = "Harry Strickland" });

            context.Set<Address>().AddRange(address1, address2, address3);

            var address21 = new Address2 { Id = "1", Street = "3 Dragons Way", City = "Meereen" };
            var address22 = new Address2 { Id = "2", Street = "42 Castle Black", City = "The Wall" };
            var address23 = new Address2 { Id = "3", Street = "House of Black and White", City = "Braavos" };

            context.Set<Person2>().AddRange(
                new Person2 { Name = "Daenerys Targaryen", Address = address21 },
                new Person2 { Name = "John Snow", Address = address22 },
                new Person2 { Name = "Arya Stark", Address = address23 });

            context.Set<Address2>().AddRange(address21, address22, address23);

            context.SaveChanges();
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:27,代码来源:OneToOneQueryFixtureBase.cs

示例5: ProductsModule

        public ProductsModule(DbContext dbContext)
        {
            Handle<AddProductModel, int>(command =>
            {
                var product = dbContext.Set<ProductModel>().Add(new ProductModel()
                {
                    //ProductModelID = 123,
                    Name = "New Product Name - " + DateTime.Now.Ticks,
                    rowguid = Guid.NewGuid(),
                    ModifiedDate = DateTime.UtcNow,
                });
                dbContext.SaveChanges(); // to get the new ProductModelId
                return product.ProductModelID;
            });

            Handle<SetProductModelName>(command =>
            {
                var product = dbContext.Set<ProductModel>().Find(1);
                product.Name = "Classic Vest " + DateTime.UtcNow;
            });

            Handle<AddProductReview>(command =>
            {
                var productReview = new ProductReview(command);
                dbContext.Set<ProductReview>().Add(productReview);
            });
        }
开发者ID:jdaigle,项目名称:CQRSPipelineDemo,代码行数:27,代码来源:ProductsModule.cs

示例6: InvoiceItemService

 public InvoiceItemService()
 {
     _repository = new invoicesEntities();
     _invoices = _repository.Set<Invoice>();
     _invoiceItems = _repository.Set<InvoiceItem>();
     _products = _repository.Set<Product>();
 }
开发者ID:txsll,项目名称:SLLInvoices,代码行数:7,代码来源:InvoiceItemService.cs

示例7: Run

        public static void Run(DbContext db)
        {
            if (!db.Set<Profile>().Any())
                db.Add(new Profile
                {
                    Name = "Alexandre Machado",
                    Email = "[email protected]",
                    Login = "cwinet\alexandrelima"
                });
            if (!db.Set<Skill>().Any())
                db.AddRange(
                    new Skill { SkillName = "ASP.NET" },
                    new Skill { SkillName = "Ruby" },
                    new Skill { SkillName = "JavaScript" }
                    );
            if (!db.Set<Mastery>().Any())
                db.AddRange(
                    new Mastery { Code = 10, Name = "Iniciante", Description = "recordação não-situacional, reconhecimento decomposto, decisão analítica, consciência monitorada" },
                    new Mastery { Code = 20, Name = "Competente", Description = "recordação situacional, reconhecimento decomposto, decisão analítica, consciência monitorada" },
                    new Mastery { Code = 30, Name = "Proeficiente", Description = "recordação situacional, reconhecimento holítico, decisão analítica, consciência monitorada" },
                    new Mastery { Code = 40, Name = "Experiente", Description = "recordação situacional, reconhecimento holítico, decisão intuitiva, consciência monitorada" },
                    new Mastery { Code = 50, Name = "Mestre", Description = "recordação situacional, reconhecimento holítico, decisão intuitiva, consciência absorvida" });

            db.SaveChanges();
        }
开发者ID:Alexandre-Machado,项目名称:SkillMap,代码行数:25,代码来源:Seed.cs

示例8: ShouldQueryBusinessDirectoryModel

        public void ShouldQueryBusinessDirectoryModel()
        {
            var model = CreateModel();
            model.Dump();

            using (var connection = CreateConnection())
            using (var context = new DbContext(connection, model.Compile(), false))
            {
                Assert.That(context.Set<Project>().ToArray(), Has.Length.EqualTo(6));
                Assert.That(context.Set<Territory>().ToArray(), Has.Length.EqualTo(5));
                Assert.That(context.Set<CategoryGroup>().ToArray(), Has.Length.EqualTo(5));
            }
        }
开发者ID:gitter-badger,项目名称:nuclear-river,代码行数:13,代码来源:EdmxBuilderModelTests.cs

示例9: ShouldQueryCustomerIntelligenceModel

        public void ShouldQueryCustomerIntelligenceModel()
        {
            var model = CreateModel();
            model.Dump();

            using (var connection = CreateConnection())
            using (var context = new DbContext(connection, model.Compile(), false))
            {
                Assert.That(context.Set<Client>().ToArray(), Has.Length.EqualTo(1));
                Assert.That(context.Set<ClientContact>().ToArray(), Has.Length.EqualTo(3));
                Assert.That(context.Set<Firm>().ToArray(), Has.Length.EqualTo(2));
                Assert.That(context.Set<FirmBalance>().ToArray(), Has.Length.EqualTo(3));
                Assert.That(context.Set<FirmCategory>().ToArray(), Has.Length.EqualTo(3));
            }
        }
开发者ID:gitter-badger,项目名称:nuclear-river,代码行数:15,代码来源:EdmxBuilderModelTests.cs

示例10: Registrar

        public void Registrar(DbContext dbContext)
        {
            RegistrarModulo();
            RegistrarOperaciones();

            dbContext.Set<Formulario>().Add(_moduloFormulario);
        }
开发者ID:CamiCasus,项目名称:FiguraManager,代码行数:7,代码来源:ModuloBase.cs

示例11: Action

 public void Action(DbContext context)
 {
     if (!context.Set<SysRole>().Any(p => p.Name == "admin"))
     {
         context.Set<SysRole>()
             .Add(new SysRole()
             {
                 Name = "admin",
                 Remark = "超级管理员角色,拥有系统最高权限",
                 IsAdmin = true,
                 IsSystem = true,
                 IsLocked = false,
                 CreatedTime = DateTime.Now
             });
     }
 }
开发者ID:liuxx001,项目名称:Bode,代码行数:16,代码来源:RoleSeedAction.cs

示例12: ShouldQueryFirms

        public void ShouldQueryFirms()
        {
            var model = CreateModel();

            using (var connection = CreateConnection())
            using (var context = new DbContext(connection, model.Compile(), false))
            {
                var firm = context.Set<Firm>()
                    .Include(x => x.Balances)
                    .Include(x => x.Categories)
                    .Include(x => x.CategoryGroup)
                    .Include(x => x.Client)
                    .Include(x => x.Client.CategoryGroup)
                    .Include(x => x.Client.Contacts)
                    .Include(x => x.Territories)
                    .OrderBy(x => x.Id)
                    .FirstOrDefault();

                Assert.That(firm, Is.Not.Null);
                Assert.That(firm.Name, Is.Not.Null.And.EqualTo("Firm 1"));
                Assert.That(firm.Balances, Is.Not.Null.And.Count.EqualTo(2));
                Assert.That(firm.Categories, Is.Not.Empty.And.Count.EqualTo(1));
                Assert.That(firm.CategoryGroup, Is.Not.Null);
                Assert.That(firm.Client, Is.Not.Null.And.Property("Name").EqualTo("Client 1"));
                Assert.That(firm.Client.CategoryGroup, Is.Not.Null);
                Assert.That(firm.Client.Contacts, Is.Not.Null.And.Count.EqualTo(3));
                Assert.That(firm.Territories, Is.Not.Empty.And.Count.EqualTo(2));
            }
        }
开发者ID:gitter-badger,项目名称:nuclear-river,代码行数:29,代码来源:EdmxBuilderModelTests.cs

示例13: Get

        public List<DataEmployees> Get()
        {
            employees = new List<DataEmployees>();
            info = new List<Info>();

            try
            {
                using(DbContext ctx = new DbContext(ConfigurationManager.ConnectionStrings["EmployeesEntities"].ConnectionString))
                {
                    info = ctx.Set<Info>().ToList();
                }

                foreach (Info i in info)
                {
                    employees.Add(new DataEmployees
                    { Name = i.Name, Address = i.Address, Post = i.Post }
                    );
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return employees;
        }
开发者ID:RoykoSerhiy,项目名称:visualStudio_projects,代码行数:27,代码来源:Service1.cs

示例14: LogRepository

        public LogRepository(DbContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            _context = context;
            _logEntity = _context.Set<Log>();
        }
开发者ID:vitorsalgado,项目名称:fatec-mobile,代码行数:7,代码来源:LogRepository.cs

示例15: UndoAll

        public static void UndoAll(DbContext context)
        {
            //detect all changes (probably not required if AutoDetectChanges is set to true)
            context.ChangeTracker.DetectChanges();

            //get all entries that are changed
            var entries = context.ChangeTracker.Entries().Where(e => e.State != EntityState.Unchanged).ToList();

            //somehow try to discard changes on every entry
            foreach (var dbEntityEntry in entries)
            {
                var entity = dbEntityEntry.Entity;

                if (entity == null) continue;

                if (dbEntityEntry.State == EntityState.Added)
                {
                    //if entity is in Added state, remove it. (there will be problems with Set methods if entity is of proxy type, in that case you need entity base type
                    var set = context.Set(entity.GetType());
                    set.Remove(entity);
                }
                else if (dbEntityEntry.State == EntityState.Modified)
                {
                    //entity is modified... you can set it to Unchanged or Reload it form Db??
                    dbEntityEntry.Reload();
                }
                else if (dbEntityEntry.State == EntityState.Deleted)
                    //entity is deleted... not sure what would be the right thing to do with it... set it to Modifed or Unchanged
                    dbEntityEntry.State = EntityState.Modified;
            }
        }
开发者ID:JesusAlb,项目名称:HelpDeskWeb,代码行数:31,代码来源:dbhelp.cs


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