本文整理汇总了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();
}
}
示例2: ConcreteDatabase
public ConcreteDatabase(
DbContext context,
IRelationalDataStoreCreator dataStoreCreator,
ILoggerFactory loggerFactory)
: base(context, dataStoreCreator, loggerFactory)
{
}
示例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));
}
}
示例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);
}
示例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);
}
示例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");
}
示例7: EntityEntryGraphIterator
public EntityEntryGraphIterator(
[NotNull] DbContext context,
[NotNull] IStateManager stateManager)
{
_context = context;
_stateManager = stateManager;
}
示例8: DbContextWrapper
public DbContextWrapper(DbContext context)
{
Context = context;
objectContext = ((IObjectContextAdapter) context).ObjectContext;
objectContext.ObjectMaterialized += ObjectMaterializedHandler;
}
示例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());
}
}
}
示例10: FindSets
protected virtual void FindSets(ModelBuilder modelBuilder, DbContext context)
{
foreach (var setInfo in SetFinder.FindSets(context))
{
modelBuilder.Entity(setInfo.EntityType);
}
}
示例11: Registrar
public void Registrar(DbContext dbContext)
{
RegistrarModulo();
RegistrarOperaciones();
dbContext.Set<Formulario>().Add(_moduloFormulario);
}
示例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));
}
示例14: GuardClause_all
public void GuardClause_all()
{
Assert.Throws<ArgumentNullException>(() =>
{
var value = new DbContext(null, null);
});
}
示例15: CurrentContext
/// <copydocfrom cref="IDbContextProvider.CurrentContext" />
public DbContext CurrentContext()
{
lock (syncLock)
{
return context ?? (context = func());
}
}