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


C# DbContext类代码示例

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


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

示例1: AddActiveDirectoryUser

        protected static void AddActiveDirectoryUser(Guid userId, string name, string accountName, bool isDisabled = false,
            IEnumerable<KeyValuePair<string, string>> customProperties = null)
        {
            using (DbContext writeDbContext = new DbContext())
            {
                User user = new User
                {
                    Id = userId,
                    Name = name,
                    IsDisabled = isDisabled,
                };

                Account account = new Account
                {
                    UserId = user.Id,
                    Name = accountName,
                    Type = AccountType.ActiveDirectory
                };

                user.Accounts.Add(account);

                if (customProperties != null)
                {
                    user.CustomProperties.AddRange(customProperties.Select(c => new CustomProperty { Id = Guid.NewGuid(), Name = c.Key, Value = c.Value }));
                }

                writeDbContext.Users.Add(user);
                writeDbContext.SaveChanges();
            }
        }
开发者ID:affecto,项目名称:dotnet-IdentityManagement,代码行数:30,代码来源:DbTests.cs

示例2: ConcreteDatabase

 public ConcreteDatabase(
     DbContext context,
     IRelationalDataStoreCreator dataStoreCreator,
     ILoggerFactory loggerFactory)
     : base(context, dataStoreCreator, loggerFactory)
 {
 }
开发者ID:thegido,项目名称:EntityFramework,代码行数:7,代码来源:RelationalDatabaseExtensionsTest.cs

示例3: 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

示例4: Store

 public void Store(DbContext dbContext)
 {
     if (HttpContext.Current.Items.Contains(DataContextKey))
         HttpContext.Current.Items[DataContextKey] = dbContext;
     else
         HttpContext.Current.Items.Add(DataContextKey, dbContext);
 }
开发者ID:chitraju-chaithanya,项目名称:inse6260,代码行数:7,代码来源:HttpDataContextStorageContainer.cs

示例5: TryGetModelHash

        /// <summary>
        ///     Attempts to get the model hash calculated by Code First for the given context.
        ///     This method will return null if the context is not being used in Code First mode.
        /// </summary>
        /// <param name = "context">The context.</param>
        /// <returns>The hash string.</returns>
        public static string TryGetModelHash(DbContext context)
        {
            //Contract.Requires(context != null);

            var compiledModel = context.InternalContext.CodeFirstModel;
            return compiledModel == null ? null : new ModelHashCalculator().Calculate(compiledModel);
        }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:13,代码来源:EdmMetadata.cs

示例6: 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

示例7: EntityEntryGraphIterator

 public EntityEntryGraphIterator(
     [NotNull] DbContext context,
     [NotNull] IStateManager stateManager)
 {
     _context = context;
     _stateManager = stateManager;
 }
开发者ID:thegido,项目名称:EntityFramework,代码行数:7,代码来源:EntityEntryGraphIterator.cs

示例8: DbContextWrapper

        public DbContextWrapper(DbContext context)
        {
            Context = context;

            objectContext = ((IObjectContextAdapter) context).ObjectContext;
            objectContext.ObjectMaterialized += ObjectMaterializedHandler;
        }
开发者ID:popcatalin81,项目名称:DataAccess,代码行数:7,代码来源:DbContextWrapper.cs

示例9: CheckValue

        public void CheckValue(DbContext ctx, object actual)
        {
            var propertyType = Accessor.MemberType;

            var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
            var entitySet = objectContext.GetEntitySet(propertyType);
            var keyMembers = entitySet.ElementType.KeyMembers;

            ctx.Entry(actual).Reference(Accessor.Name).Load();

            var actualEntity = Accessor.GetValue(actual);
            if (actualEntity == null)
            {
                throw new AssertionException(ExpectedEntity.Dump(), "NULL");
            }
            foreach (var keyMember in keyMembers)
            {
                var accessor = new PropertyAccessor(propertyType.GetProperty(keyMember.Name));

                var actualKeyValue = accessor.GetValue(actualEntity);
                var expectedKeyValue = accessor.GetValue(ExpectedEntity);

                if (!expectedKeyValue.Equals(actualKeyValue))
                {
                    throw new AssertionException(ExpectedEntity.Dump(), actualEntity.Dump());
                }
            }
        }
开发者ID:kmcginnes,项目名称:EFSpecs,代码行数:28,代码来源:Reference.cs

示例10: FindSets

 protected virtual void FindSets(ModelBuilder modelBuilder, DbContext context)
 {
     foreach (var setInfo in SetFinder.FindSets(context))
     {
         modelBuilder.Entity(setInfo.EntityType);
     }
 }
开发者ID:aishaloshik,项目名称:EntityFramework,代码行数:7,代码来源:ModelSource.cs

示例11: Registrar

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

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

示例12: EntityFrameworkExternalDataSource

 public EntityFrameworkExternalDataSource(DbContext dbContext)
 {
     _dbContext = dbContext;
     SetOptionsForDbContext(_dbContext);
     _efManager = new EFManager(dbContext);
     _efManager.ReloadDbEntries();
 }
开发者ID:GigaSpaces-ProfessionalServices,项目名称:xapnet-templates,代码行数:7,代码来源:EntityFrameworkExternalDataSource.cs

示例13: UserService

 public UserService(DbContext context, IRepository<User> users, IRepository<Message, int> messages)
 {
     this.users = users;
     this.messages = messages;
     this.userManager = new UserManager<User>(new UserStore<User>(context));
     this.roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
 }
开发者ID:atanas-georgiev,项目名称:ASP.NET-MVC-Final-Project,代码行数:7,代码来源:UserService.cs

示例14: GuardClause_all

		public void GuardClause_all()
		{
			Assert.Throws<ArgumentNullException>(() =>
			{
				var value = new DbContext(null, null);
			});
		}
开发者ID:gongdo,项目名称:orientdb-http-dnx,代码行数:7,代码来源:DbContextTests.cs

示例15: CurrentContext

 /// <copydocfrom cref="IDbContextProvider.CurrentContext" />
 public DbContext CurrentContext()
 {
     lock (syncLock)
     {
         return context ?? (context = func());
     }
 }
开发者ID:kevinwiegand,项目名称:EnergyTrading-Core,代码行数:8,代码来源:DbContextProvider.cs


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