entity-framework-6

Multiple .Where() clauses on an Entity Framework Queryable

試著忘記壹切 提交于 2019-12-01 17:02:58
I am trying to implement a complex filter using Entity Framework: I want to add where clauses to the queryable object based on my provided search criteria. Can I do the following in Entity Framework 6? var queryable = db.Users.Where(x => x.Enabled && !x.Deleted); // Filter var userId = User.Identity.GetUserId(); queryable.Where(x => x.AspNetUser.Id == userId); queryable.Where(x => x.Status >= 2); // ...etc I know that I can do: var queryable = db.Users .Where(x => x.Enabled && !x.Deleted) .Where(x => x.AspNetUser.Id == userId) .Where(x => x.Status >= 2); But I am not getting the expected

Entity Framework 6 Where in Bulk Update/Delete in one transaction

回眸只為那壹抹淺笑 提交于 2019-12-01 16:59:27
问题 In EF6, I want to update/delete bulk data in one query. My code is using (var context = _dataContextFactory.GetContext()) { var result1 = from b in context.MyTables where new List<int> {592, 593, 594}.Contains(b.Id) select b; foreach (var item in result1 ) { item.StatusId = 3; } context.SaveChanges(); } But in Sql Profiler, there are three scripts exec sp_executesql N'UPDATE [dbo].[MyTable] SET [StatusId] = @0 WHERE ([Id] = @1) ',N'@0 int,@1 int',@0=1,@1=592 exec sp_executesql N'UPDATE [dbo].

Entity Framework 6 with SQL Server 2012 gives System.Data.Entity.Core.ProviderIncompatibleException

核能气质少年 提交于 2019-12-01 16:43:32
问题 I have Visual Studio 2012 and I'm using the Entity Framework stack with EF 6. I did all correct but while adding migration I am getting the error . System.Data.Entity.Core.ProviderIncompatibleException Here are the classes public class Order { public virtual int OrderID { get; set; } } The context file public ShoppingCartContext() : base("ShoppingCartDb") { Database.SetInitializer<ShoppingCartContext>(new DropCreateDatabaseAlways<ShoppingCartContext>()); } protected override void

Value cannot be null: connection while testing database

末鹿安然 提交于 2019-12-01 16:38:42
I'm trying to run integration tests for my ASP.NET MVC application using Entity Framework 6. The error I get is System.Data.Entity.Core.EntityException: The underlying provider failed on Rollback. ---> System.ArgumentNullException: Value cannot be null. Parameter name: connection The code looks like this: Database.SetInitializer(new PrimaryInitializerTest()); _context = new PrimaryContextTest(); _context.Database.Initialize(true); using (var dbt = _context.Database.BeginTransaction()) { dbt.Commit(); dbt.Rollback(); } I also tried having an dbt.UnderlyingTransaction.Connection.Open() call just

TransactionScope and SQLite database gets locked

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 16:35:47
问题 I am trying to use Entity Framework 6 with SQLite and running into a database locked issue when trying to use TransactionScope . Here is my code: using (var txn = new TransactionScope()) { using (var ctx = new CalibreContext()) { var book = ctx.Books.First(x => x.Id == 2); var author = ctx.Authors.First(x => x.Id == 3); book.Authors.Add(author); ctx.SaveChanges(); } txn.Complete(); } First line var book = ctx.Books.First(x => x.Id == 2); executes ok. but then once I move on to the next one I

Does Identity Owin require LazyLoading?

半世苍凉 提交于 2019-12-01 16:04:39
tl;dr: Identity seems to require that LazyLoading NOT be disabled; is this true, and what is the cleanest workaround? I did some basic A-B testing on a simple C# ASP.NET 4.5.1 MVC-5 web application using EntityFramework 6.0.2, Identity EntityFramework 1.0.0, and Identity Owin 1.0.0 and it appears that Owin requires that lazy loading is NOT disabled in the ApplicationContext constructor. To replicate the issue just make a quick MVC app using Visual Studio 2013, use the MVC template, leave everything at the defaults except uncomment the line: 'app.UseGoogleAuthentication();' in App_Start/Startup

Does Identity Owin require LazyLoading?

*爱你&永不变心* 提交于 2019-12-01 15:57:29
问题 tl;dr: Identity seems to require that LazyLoading NOT be disabled; is this true, and what is the cleanest workaround? I did some basic A-B testing on a simple C# ASP.NET 4.5.1 MVC-5 web application using EntityFramework 6.0.2, Identity EntityFramework 1.0.0, and Identity Owin 1.0.0 and it appears that Owin requires that lazy loading is NOT disabled in the ApplicationContext constructor. To replicate the issue just make a quick MVC app using Visual Studio 2013, use the MVC template, leave

EF 6 with a dnx project

这一生的挚爱 提交于 2019-12-01 15:57:18
I have a new ASP.net 5 dnx class library I am using for entity framework. I need to target EF 6 because some features I need are not in EF 7. First the EF tools (like enable-migration) were not there. I added an old style class library and installed EF 6 and now the commands are there. When I run enable migrations I get this error: PM> Enable-Migrations Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral

Value cannot be null: connection while testing database

本秂侑毒 提交于 2019-12-01 15:16:37
问题 I'm trying to run integration tests for my ASP.NET MVC application using Entity Framework 6. The error I get is System.Data.Entity.Core.EntityException: The underlying provider failed on Rollback. ---> System.ArgumentNullException: Value cannot be null. Parameter name: connection The code looks like this: Database.SetInitializer(new PrimaryInitializerTest()); _context = new PrimaryContextTest(); _context.Database.Initialize(true); using (var dbt = _context.Database.BeginTransaction()) { dbt

Can't auto-generate IDENTITY with AddRange in Entity Framework

守給你的承諾、 提交于 2019-12-01 14:18:26
问题 I don't know if it's an Entity Framework's desing choice or a wrong approach on my behalf, but whenever I try to AddRange entities to a DbSet I can't seem to get the auto-generated IDENTITY fields. [Table("entities")] public class Entity { [Key] [Column("id")] public long Id { get; set; } [Column("field")] public string Field { get; set; } } var entities = new Entity[] { new Entity() { Field = "A" }, new Entity() { Field = "B" }, }; _dbContext.Entities.AddRange(entities); await _dbContext