dbcontext

Entity Framework: multiple DB having the same schema

ⅰ亾dé卋堺 提交于 2019-12-06 14:59:49
I have just created an ASP.NET MVC 4 & WebAPI project. After that I have added .edmx data source to project. I have multiple databases with the same schema. Dynamically I want to replace connection string using default constructor provided in EF. But in Model1.Designer.cs , every time I get error like "Member with same signature already declared". I'm unable to solve this problem. Yes, it works! All you need to change is the connection string . And I have just tested it in order to satisfy my own curiosity. Here are the steps that I took: 1. Take an existing database and create a model for it.

Merge IdentityDbContext and DbContext ASP.NET MVC

本秂侑毒 提交于 2019-12-06 11:36:56
I would like to only have one DbContext in my ASP.NET MVC project. How should I merge the default IdentityDbContext with my own code first DbContext? They are using the same database. public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("SignalRChat") { } } This is the default one provided in the IdentityModel.cs class. Is it safe to do the following? And append my own DbSets? public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("SignalRChat") { } public DbSet<Student> Students {

Adapter pattern for IDbSet properties of a DbContext class

[亡魂溺海] 提交于 2019-12-06 10:20:56
Is there a way to use the method described in this answer No FindAsync() method on IDbSet for DbSet properties of a DbContext? Edit: The answer linked contains a description how to build a interface inheriting from IDbSet and adding support for the SearchAsync method of the DbSet class. I understand everything which Keith Payne has written, but I don’t know how I can use it in DbContext. For example I’ve a DbContext which looks something like this: public class MyContext : DbContext { public DbSet<Customer> Customers { get; set; } } How can I use MyDbSet (class described in the answer.) or a

dbcontext loading related entities using select(), include() and where() not working

旧街凉风 提交于 2019-12-06 08:02:47
I have the following relationship between the entities. Company 1 ---* Appointments *---1 Employee I have the .net asp membership in a separate database. Whenever a user is created it can be assigned to companies, employees, or administrators roles. in the Index action of my Company Controller, I check the logged in user's role. Based on the role, I make different linq query. For example, administrators can get list of all companies, companies can get list of company which has a username property (string) same as the User.Identity.Name. For both of administrators and companies role, it is

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

我是研究僧i 提交于 2019-12-06 07:16:32
问题 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

should EF dbContext be created on every transaction

巧了我就是萌 提交于 2019-12-06 05:53:22
I'm trying to figure out the best way to manage the DbContext. I've seen code samples that don't dispose and I've seen people say that that is a bad idea. Is it appropriate for me to do something like below? Also, should I put every transaction, including reads, in a new DbContext? This might be another question, but is the part about the EntityState necessary? public abstract class GenericRepository<T> where T : EntityData { protected MyDbContext Context { get { return new MyDbContext(); } } public T Save(T obj) { T item; using (var context = Context) { var set = context.Set<T>(); if (String

Dispose method in web api 2 web service

早过忘川 提交于 2019-12-06 04:02:56
问题 I am coding an MVC 5 internet application with a web api 2 web service. Do I need a dispose method for the DbContext class in a web service? It is not there as default. 回答1: Actually, System.Web.Http.ApiController already implements IDisposable : // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // ... public abstract class ApiController : IHttpController, IDisposable { // ... #region IDisposable public void

DbContext ChangeTracker: Id of Added Entity for Auditing

*爱你&永不变心* 提交于 2019-12-06 03:48:18
问题 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

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

痞子三分冷 提交于 2019-12-06 03:26:07
问题 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 回答1: The exception should be thrown immediately when you call Set

DbContext and Connection pools

我怕爱的太早我们不能终老 提交于 2019-12-06 03:01:09
问题 In an application that I've inherited there's this in a Base Controller, that every other controller in the application inherits from. public BaseController() { db = new MyDbContext(); db.Database.Log = s => Debug.Write(s); } public MyDbContext() : base("name=MyDbContext") { // hack to force Visual Studio to deploy the Entityframework.SqlServer package var instance = SqlProviderServices.Instance; } Due to the way the application has been designed, at least 2 contexts are created per request.