dbcontext

DbContext has been disposed

不想你离开。 提交于 2019-12-05 04:34:23
I developed a web application with ASP.NET MVC 4 and SQL Server 2008, I create ContextManager class to have only one database context in all pages. public static class ContextManager { public static HotelContext Current { get { var key = "Hotel_" + HttpContext.Current.GetHashCode().ToString("x") + Thread.CurrentContext.ContextID.ToString(); var context = HttpContext.Current.Items[key] as HotelContext; if (context == null) { context = new HotelContext(); HttpContext.Current.Items[key] = context; } return context; } } } It works properly in most of the pages, but in registration page something

How Can I Use Custom Validation Attributes on Child Models of a DB Entity?

丶灬走出姿态 提交于 2019-12-05 00:15:35
Summary: I want a data annotation validator to reference another property in the same class ( TitleAuthorAndPublishingConfiguration ). However, DB.SaveChanges() is not being called on this class directly. Rather it is being called on the parent of this class ( WebsiteConfiguration ). Therefore validationContext.ObjectType is returning WebsiteConfiguration and I am unable to refer to properties of TitleAuthorAndPublishingConfiguration within the data annotation validator. WebsiteConfiguration.cs public class WebsiteConfiguration { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

Microsoft.Data.Entity.Design, Version=10.0.0.0 is for Visual Studio Developer Preview 11

放肆的年华 提交于 2019-12-04 18:36:55
I am trying something here but keep failing. I have Visual Studio Developer Preview installed on a Windows Server 2008 R2. As you know, for now VS extensions are not compatible with VS 11 Dev Preview but I found a workaround: Visual Studio Extensions and Visual Studio 11 Dev. Preview I installed ADO.NET C# DbContext Generator on VS 11 but when I try to run the code generation, I am getting the following error: Error: this template attempted to load component assembly 'Microsoft.Data.Entity.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. For more information on this

Defining data annotation using DbContext versus Objectcontext in the database first approach

时间秒杀一切 提交于 2019-12-04 14:54:59
I am using the database first approach with entity framework, when i used to work on the default template the database tables were mapped using the ObjectContext, so i used to create #partial classes & [MetadataType(typeof ) to apply the data annotation ,, but when i start using the Dbcontext code generation template to map the database tables i found that it will create .tt folder in my Model area were i find that i can apply the data annotation directly to the .cs classes themselves without the need to create partial classes as in objectcontext case . Currently the data annotations are

Splitting DbContext into multiple contexts with overlapping DbSets

五迷三道 提交于 2019-12-04 12:31:24
问题 I have a DbContext that houses +80 entities at the moment with only 4 of the main modules done, but there are 3 more to go, and they are quite bigger so it will get up to 150 easy. I think it is the perfect time to divide the contexts. Every module uses it's own entities and will get it's own context, but there is a group of entities that are used by all of the modules, so here is mu question: Should I have one MainContext that will have all of the overlapping entities but then: What will

EF Core / DbContext > Map custom type as primary key

主宰稳场 提交于 2019-12-04 10:56:34
Using the fluent api, how do I map a custom type as the primary key within my OnModelCreating method of the DbContext class? Using EF Core I'm trying to build a model for the follow entity. public class Account { public AccountId AccountId { get; } public string Name { get; set; } private Account() { } public Account(AccountId accountId, string name) { AccountId = accountId; Name = name; } } Where the primary key is the AccountId ; the type is a simple value object like this. public class AccountId { public string Id { get; } public AccountId(string accountId) { Id = accountId; } } Within

How to check whether DbContext.Set<T> exists in model?

穿精又带淫゛_ 提交于 2019-12-04 07:43:39
I have a situation where I may be working with multiple DbContexts that may or may not contain a DbSet of SomeEntity. Naturally, if I fire off SaveChanges and this entity is not present, the following error will occur: The entity type SomeEntity is not part of the model for the current context. How can I check whether the entity or entity set exists in a model and short-circuit the offending bit of code if it does not? Richard The exception should be thrown immediately when you call Set<NotMappedEntityType> so the simplest way is to catch the exception and handle it as you need. The complex

Lazy Loading Not Working After SaveChanges Entity Framework

别等时光非礼了梦想. 提交于 2019-12-04 07:37:38
In the function below, after the context.SaveChanges(), the entity PropertyType is always null. I just converted from using ObjectContext to DBContext (with database first) and before the change, it worked fine and now it doesn't. Is there something I'm missing? I check the PropertyTypeID and it is written correctly and exists in the db. All relationships are correctly setup in the db and edmx file. The generated .tt files show the PropertyType object as virtual. This is EF 5. Here is the code (non-important assignments of entity properties has been removed): private ListingTransferDetail

DbContext ChangeTracker: Id of Added Entity for Auditing

折月煮酒 提交于 2019-12-04 07:30:57
I have something like this: public override int SaveChanges() { foreach (var changeLog in this.ChangeTracker.Entries() .Where(p => p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified) .SelectMany(entity => AuditRecords(entity))) { this.ChangeLogs.Add(changeLog); } return base.SaveChanges(); } But, of course, audited change logs will not contain the primary key value of the entity when the EntityState is Added (until after SaveChanges). How can I obtain the primary key value for change auditing purposes? Richard I'd store the references to the

How to update an existing Entity from ViewModel using Automapper and EF4 DbContext with Lazy Loading enabled

六月ゝ 毕业季﹏ 提交于 2019-12-04 04:56:52
I am using Automapper to map between Entity and ViewModel object (in both directions). The model uses EF4 DbContext POCOs and needs LazyLoading (and therefore Proxy Generation) enabled. I have come across a problem attempting to update an existing entity from a viewmodel. When I call Mapper.Map(vm, entity), Automapper throws an exception. My question is: how are you supposed to work with EF Proxy objects using Automapper? The code looks (simplified) like this: public class MyEntity { public int Id {get;set;} public int Name {get;set;} } public class ViewModel { public int Id {get;set;} public