dbcontext

Entity Framework Code First: SaveChanges is not atomic

别说谁变了你拦得住时间么 提交于 2019-12-03 09:45:17
问题 I have the following very simple unit test that reproduces a case where DbContext.SaveChanges is not atomic. By not atomic I mean that the committed data can be read before all the commit was completed. Add task: In a loop, adds a new TestEntity and a ReferencingEntity. Validate task: checks if there is a TestEntity that is not referenced by any ReferencingEntity - that is not supposed to happen because of the way I add the entities. The unit test fails... any advice? EDIT: According to the

Is there an EF6 DbContext Generator for C# which uses Fluent API?

这一生的挚爱 提交于 2019-12-03 09:20:03
I know that the EF6 VS Tools for Visual Studio 2012 come with a T4 template to generate DbContext classes which work with EF6. But I want to have a generator which uses fluent API. The older version I used with EF4 and EF5 don't work with EF6 and the author no longer works on them to make them EF6 compatible. Is anyone else working on a generator which uses Fluent API which works with EF6? http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d If you have a database you can use reverse code first it will generate all your fluent api mapping. 来源: https:/

Entity Framework Core 1.0 DbContext not scoped to http request

孤者浪人 提交于 2019-12-03 08:42:16
I understood by watching this video with Rowan Miller https://channel9.msdn.com/Series/Whats-New-with-ASPNET-5/06 (at minute 22) that the way of configuring Entity Framework Core (previously known as EF7) into an ASP.NET Core 1.0 app (previously known as ASP.NET 5) in Startup.cs is as follows: public void ConfigureServices(IServiceCollection services) { //Entity Framework 7 scoped per request?? services.AddEntityFramework() .AddSqlServer() .AddDbContext<MyDbContext>(options => { options .UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]); }); //MVC 6 services.AddMvc(); }

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

…衆ロ難τιáo~ 提交于 2019-12-03 08:32:46
问题 I have a .NET Core 1.1 API with EF Core 1.1 and using Microsoft's vanilla setup of using Dependency Injection to provide the DbContext to my services. (Reference: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro#register-the-context-with-dependency-injection) Now, I am looking into parallelizing database reads as an optimization using WhenAll So instead of: var result1 = await _dbContext.TableModel1.FirstOrDefaultAsync(x => x.SomeId == AnId); var result2 = await _dbContext

Why does Entity Framework automatically use the ObjectContext instead of the DbContext when mapping database tables using ADO.NET Entity datamodel

你。 提交于 2019-12-03 07:24:39
问题 I am following the database approach first; I have created the tables in my SQL Server 2008 database, then I map those tables to Entity Framework classes using an ADO.NET Entity Data Model. But when I opened the designer.cs file I found the following code in the class definition which was created automatically: public partial class PortalEntities : ObjectContext so I have the following three question that get my confused: Why does my PortalEntities class derive from ObjectContext and not

Using multiple DbContexts with a generic repository and unit of work

坚强是说给别人听的谎言 提交于 2019-12-03 05:23:44
问题 My application is getting larger and so far I have a single MyDbContext which has all the tables I need in my application. I wish (for the sake of overview) to split them up into multiple DbContext , like MainDbContext , EstateModuleDbContext , AnotherModuleDbContext and UserDbContext . I am unsure how this is done probably as I am right now using dependecy injection (ninject) to place my DbContext on my UnitOfWork class like: kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork<MyDbContext>

EF (entity framework) usage of “using” statement

江枫思渺然 提交于 2019-12-03 03:41:41
问题 I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where " using " statement is used, i.e. public Item GetItem(long itemId) { using (var db = new MyEntities()) { return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault(); } } Here we create a new instance of DBcontext MyEntities() . We using " using " in order to "ensure the correct use of IDisposable objects." It's only one method in my

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

╄→гoц情女王★ 提交于 2019-12-03 01:31:45
I have a .NET Core 1.1 API with EF Core 1.1 and using Microsoft's vanilla setup of using Dependency Injection to provide the DbContext to my services. (Reference: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro#register-the-context-with-dependency-injection ) Now, I am looking into parallelizing database reads as an optimization using WhenAll So instead of: var result1 = await _dbContext.TableModel1.FirstOrDefaultAsync(x => x.SomeId == AnId); var result2 = await _dbContext.TableModel2.FirstOrDefaultAsync(x => x.SomeOtherProp == AProp); I use: var repositoryTask1 = _dbContext

Multiple connection to DbContext in EFCore and .net Core2.0

只谈情不闲聊 提交于 2019-12-03 00:51:46
问题 I have facing a problem to connecting to DBcontext with multiple connection. please help me out from this problem , here is me scenario . I have an Angular5 application and Webapi Controllers. I have 4 databases and Each database has different users . When 4 user are trying to connecting to their database at Same time through Webapi controller , that time all users are getting data from single database (instead of getting different database ) . When we try to Connecting one by one then data

Entity Framework Code First: SaveChanges is not atomic

萝らか妹 提交于 2019-12-03 00:22:10
I have the following very simple unit test that reproduces a case where DbContext.SaveChanges is not atomic. By not atomic I mean that the committed data can be read before all the commit was completed. Add task: In a loop, adds a new TestEntity and a ReferencingEntity. Validate task: checks if there is a TestEntity that is not referenced by any ReferencingEntity - that is not supposed to happen because of the way I add the entities. The unit test fails... any advice? EDIT: According to the accepted answer - In order to run the unit test with the proposed solution add in the InitTest method: