dbcontext

Entity Framework Core Using multiple DbContexts

霸气de小男生 提交于 2019-11-27 00:14:36
问题 I'm having a problem that when I try to access a field in my PartsDbContext I get the following error: System.Data.SqlClient.SqlException: 'Invalid object name 'fieldName'' It seems that this is because I'm trying to make my PartsDbContext use the same database as my ApplicationDbContext which is used with Identity. I need to know how to setup a 2nd dbcontext to work with EF core that uses/creates a different database. I've tried creating a 2nd connection string but that gets me this error:

How to mock DbContext [duplicate]

牧云@^-^@ 提交于 2019-11-26 22:10:45
问题 This question already has an answer here : Unit Testing and Mocking using RhinoMocks (1 answer) Closed 5 years ago . Here is the code I want to test public DocumentDto SaveDocument(DocumentDto documentDto) { Document document = null; using (_documentRepository.DbContext.BeginTransaction()) { try { if (documentDto.IsDirty) { if (documentDto.Id == 0) { document = CreateNewDocument(documentDto); } else if (documentDto.Id > 0) { document = ChangeExistingDocument(documentDto); } document =

Using DbContext Set<T>() instead of exposing on the context

孤者浪人 提交于 2019-11-26 20:48:34
问题 Are there any differences when doing the following: public class UsersContext : DbContext { public DbSet<User> Users { get; set; } } versus using the Set<T> method of the context: public class UsersContext : DbContext { } var db = new UsersContext(); var users = db.Set<User>(); These effectively do the same thing, giving me a set of Users, but are there any big differences other than you are not exposing the set through a property? 回答1: The Users property is added for convenience, so you don

Can you get the DbContext from a DbSet?

安稳与你 提交于 2019-11-26 20:34:38
问题 In my application it is sometimes necessary to save 10,000 or more rows to the database in one operation. I've found that simply iterating and adding each item one at a time can take upwards of half an hour. However, if I disable AutoDetectChangesEnabled it takes ~ 5 seconds (which is exactly what I want) I'm trying to make an extension method called "AddRange" to DbSet which will disable AutoDetectChangesEnabled and then re-enable it upon completion. public static void AddRange<TEntity>(this

Get Auto Identity Key after Insert via EF

回眸只為那壹抹淺笑 提交于 2019-11-26 19:33:57
问题 Is there a straight forward way of retrieving a DB auto generated primary key when adding a record via Entity Framework 4.1? For example: dbcontext.Entity_Tables.Add(new Entity_Table { item1 = val1, item2 = val2 }); dbcontext.SaveChanges(); newPK = ???; The SQL equivalent would be: newPK = executeOnDB("INSERT INTO Entity_Table (item1, item2) VALUES (val1, val2);SELECT @@Indentity";); BTW I'm using MySQL but the SQL would be the same as on MSSQL 回答1: I believe EF should update your entity

What is the best way to instantiate and dispose DbContext in MVC?

北城以北 提交于 2019-11-26 19:23:24
问题 MVC 3 + EF 4.1 I'm choosing between two approaches to deal with DbContext: Instantiate in Application_BeginRequest , put it into HttpContext.Current.Items and dispose in Application_EndRequest . Create disposable UnitOfWork (kindof wrapper for DbContext ) and start each controller action with using(var unitOfWork = new UnitOfWork()) { ... } Share your experience please: Which one would you prefer? what are pros and cons for each approach? 回答1: I would suggest you use a Dependency Injection

LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface

。_饼干妹妹 提交于 2019-11-26 17:21:23
I have the following generic extension method: public static T GetById<T>(this IQueryable<T> collection, Guid id) where T : IEntity { Expression<Func<T, bool>> predicate = e => e.Id == id; T entity; // Allow reporting more descriptive error messages. try { entity = collection.SingleOrDefault(predicate); } catch (Exception ex) { throw new InvalidOperationException(string.Format( "There was an error retrieving an {0} with id {1}. {2}", typeof(T).Name, id, ex.Message), ex); } if (entity == null) { throw new KeyNotFoundException(string.Format( "{0} with id {1} was not found.", typeof(T).Name, id))

EF6 DBContext Dynamic Connection String

ぃ、小莉子 提交于 2019-11-26 16:15:31
问题 public partial class ProcessContext : DbContext { static ProcessContext() { Database.SetInitializer<ProcessContext>(null); } public ProcessContext() : base("Name=ProcessCS") //Comes from Config File { } --DBSets protected override void OnModelCreating(DbModelBuilder modelBuilder) { --Code } } This is a Multi Tenent DB where we have 3 Different DB's. Centralized DB is in common location and would not be changed. This is where rest of the DB details will be stored. I need to create the

Entity Framework 5 deep copy/clone of an entity

荒凉一梦 提交于 2019-11-26 15:53:39
I am using Entity Framework 5 ( DBContext ) and I am trying to find the best way to deep copy an entity (i.e. copy the entity and all related objects) and then save the new entities in the database. How can I do this? I have looked into using extension methods such as CloneHelper but I am not sure if it applies to DBContext . Leo One cheap easy way of cloning an entity is to do something like this: var originalEntity = Context.MySet.AsNoTracking() .FirstOrDefault(e => e.Id == 1); Context.MySet.Add(originalEntity); Context.SaveChanges(); the trick here is AsNoTracking() - when you load an

c# entity framework: correct use of DBContext class inside your repository class

自闭症网瘾萝莉.ら 提交于 2019-11-26 15:29:31
问题 I used to implement my repository classes as you can see below public Class MyRepository { private MyDbContext _context; public MyRepository(MyDbContext context) { _context = context; } public Entity GetEntity(Guid id) { return _context.Entities.Find(id); } } However I recently read this article which says that's a bad practice to have data context as a private member in your repository: http://devproconnections.com/development/solving-net-scalability-problem Now, theoretically the article is