entity-framework-core

Entity Framework Core 1.0 Connection Strings

纵然是瞬间 提交于 2019-12-05 00:20:51
We are working on a vary large ASP.NET Core MVC 1.0 application. We have 4-tiers to each of our applications as follows: DTO Repository (Entity Framework - Code First) Service (Business Logic) MVC (UI-MVC) Currently, in our repositories, which handle all database operations we have hard coded the database connection strings in the DbContext as follows: protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Data Source=somedatabase.database.windows.net;Initial Catalog=database;Integrated Security=False;User ID=username;Password=password

How to set command timeout in aspnetcore/entityframeworkcore

微笑、不失礼 提交于 2019-12-04 23:43:40
The place where the command timeout is set is no longer the same as earlier versions. However, I cannot find anywhere that says how to change this. What I am doing is uploading very large files which takes longer than the default 30 seconds to save. Note that I ask about Command Timeout, not Migration Timeout as in another question. Carl Sharman If you're using the DI container to manage the DbContext (i.e. you're adding the DbContext to the service collection), the command timeout can be specified in the options. In Startup.ConfigureServices: services.AddDbContext<YourDbContext>(options =>

What logic determines the insert order of Entity Framework 6

时光怂恿深爱的人放手 提交于 2019-12-04 23:30:48
So, I have a DBContext, and I am doing the following operations: dbContext.SomeTables1.Add(object1) dbContext.SomeTables2.AddRange(objectArray2) dbContext.SomeTables3.AddRange(objectArray3) dbContext.SaveChanges(); The EF doesn't insert the db records in this order, it inserts them in a random order. To insert them in the same order, I have to do a dbContext.SaveChanges() after each addition. This is not an efficient solution and in my case, it is taking 10 seconds to do all my inserts, while the random order with one save takes around 3 seconds. N.B. I need the right order to solve a deadlock

InvalidOperationException “get_MetadataToken() cannot be used on the current platform” when creating tables

删除回忆录丶 提交于 2019-12-04 23:29:53
问题 This is an interesting problem for me. On my work machine, my code works perfectly fine, but on my home machine when starting with a new sqlite file I get an InvalidOperationException when db.Database.Migrate() is called. According to Windows Update , both machines are up to date. Visual Studio 2015 also shows as up to date on both machines. I'm running EF7 rc1-final The stacktrace isn't exactly deep: System.InvalidOperationException occurred HResult=-2146233079 Message=The API 'System

How to allow migration for a console application?

限于喜欢 提交于 2019-12-04 23:25:33
问题 When using asp.net core and ef core, I have no problem when invoking add-migration init . But when I apply the same approach on a console application below, I got an error saying: Unable to create an object of type 'StudentContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. What is the easiest way to fix this issue? The .net core console application project is

Entity Framework core .Include() issue

巧了我就是萌 提交于 2019-12-04 23:18:55
Been having a play about with ef core and been having an issue with the include statement. For this code I get 2 companies which is what i expected. public IEnumerable<Company> GetAllCompanies(HsDbContext db) { var c = db.Company; return c; } This returns [ { "id":1, "companyName":"new", "admins":null, "employees":null, "courses":null }, { "id":2, "companyName":"Test Company", "admins":null, "employees":null, "courses":null } ] As you can see there are 2 companies and all related properties are null as i havnt used any includes, which is what i expected. Now when I update the method to this:

How to refresh an Entity Framework Core DBContext?

橙三吉。 提交于 2019-12-04 23:18:45
When my table is updated by another party, the db context in dotnet core still return the old value, how can I force the Db context to refresh? I've done research but I only found people use Reload method, which is not available in EF core, to force the context to refresh. Some other solution suggests dispose the context after using, but I get error saying the DB context is created by dependency injection and I should not mess up with it. Dependency Injection and DbContext You mention that when you try to recreate your DbContext , you get an error about the context being managed by your

Execute SQL command in Entity Framework Core 2.0 to delete all data in a table

倾然丶 夕夏残阳落幕 提交于 2019-12-04 22:51:38
I want to execute an SQL command from Entity Framework Core 2.0, but I can't figure out how to do so. 1.- The reason why I need to, is that I want to delete all data from a database table, and using Context.remove or Context.removeRange would produce many calls to DB (one for each data in the table). 2.- I've read that there is a method .ExecuteSqlCommand to accomplish that, but that method is not present in my Context.Database (maybe in Core 2.0 it was removed?). Here is the source of the info: Dropping table In Entity Framework Core and UWP So, basically I need to delete a table from code

Execute non-query procedure not working asp.net core

血红的双手。 提交于 2019-12-04 21:15:15
I want to execute a stored procedure which returns three values (Email, Name, CompanyID) and get one parameter (CompanyID) but it's not working. I have created a class with these properties and a stored procedure which returns the data. By it is showing DatabaseFacade error. My code is: List<MyClass> AppUser = new List<MyClass>(); //Class with three properties SqlParameter param1 = new SqlParameter("@CompanyID", CompanyID); AppUser = _context.Database.SqlQuery<CC>("GetUserAndRolesForCompany", param1).ToList(); Showing this error: I have include System.Linq 'DatabaseFacade' does not contain a

ASP.NET Core only returning first value of a collection in an entity

不问归期 提交于 2019-12-04 21:12:53
I have a class MyEntity with a collection within it: public class MyEntity { [Key] public int MyId { get; set; } [Display(Name = "Process Name")] public string ProcessName { get; set; } public ICollection<Step> ProcessSteps { get; set; } } A one-many relationship with class Step: public class Step { ... [ForeignKey("MyEntityForeignKey")] public MyEntity { get; set; } } Below is the API call to return a specified MyEntity: public async Task<IActionResult> MyEntity(int? id) { if (id == null) { return Ok(await _context.MyEntity.ToListAsync()); } var process = _context.MyEntity.Where(f => f.MyId =