entity-framework-core

Entity Framework 7 IDENTITY_INSERT

懵懂的女人 提交于 2019-12-02 01:08:57
I need to store a large amount of user data in a database where every user already has an ID as a key. However, these users all out of a larger pool of users, therefore, the ID does not increment and cannot be automatically generated. I need to be able to manually set the ID. Is there an easy way to do this? I've made some tests and this seems to work for me on EF 7.0.0 rc1-final: modelBuilder.Entity<Foo>().HasKey(x => x.Id); modelBuilder.Entity<Foo>().Property(x => x.Id).ValueGeneratedNever(); My test code: static void Main(string[] args) { using (var db = new FooContext()) { var myFoo = new

Entity Framework Core - Best way to access a collection on an entity?

不打扰是莪最后的温柔 提交于 2019-12-02 01:04:00
I have a simple ASP.NET Core 2.0 + Entity Framework Core project: I have an entity called ApplicationUser , which extends Microsoft.AspNet.Identity.IdentityUser , and the ApplicationUser has a collection of another entity called Books . However, when I try to read the collection of Books from the ApplicationUser after retrieving it from UserManager: ApplicationUser user = await _userManager.GetUserAsync(User); return user.Books; I get an empty collection. If I do this instead: ApplicationUser user = await _userManager.GetUserAsync(User); return _context.Users.Include(d => d.Books)

This version of the Entity Framework Core Package Manager Console Tools doesn't support these types of projects

夙愿已清 提交于 2019-12-02 00:55:13
问题 After updating an existing project to ASP.NET Core 1.1 and Entity Framework Core 1.1 using this tutorial I tried to execute "Add-Migration MigrationName" in Package Management Console but got an error: Startup project 'src\ProjectName' is an ASP.NET Core or .NET Core project for Visual Studio 2015. This version of the Entity Framework Core Package Manager Console Tools doesn't support these types of projects. I am using VS 2015 Update 3. Project.json { "dependencies": { "CoursesManagement.DAL

How to translate this SQL query to a LINQ query in EF Core?

半世苍凉 提交于 2019-12-02 00:32:43
I have the following table : Indicators(A INT, B INT, C INT, D INT, TimeInsertedLocal DateTime) . And I have the EF Core mapping entity that maps to this table. I need to translate this SQL query to ef core Linq equivalent query . SELECT A, B, C, D, TimeInsertedLocal FROM Indicators WHERE TimeInsertedLocal >= ( SELECT MAX(I.TimeInsertedLocal) FROM Indicators AS I ) and this is the entity : public class Indicator { public int A { get; set; } public int B { get; set; } public int C { get; set; } public int D { get; set; } public DateTime TimeInsertedLocal { get; set; } } How to write the LINQ

How to detect when EF Core must do some of the IQueryable operations in memory

◇◆丶佛笑我妖孽 提交于 2019-12-02 00:27:57
问题 I've been going through my application, and there are times where only part of the IQueryable is actually translated into a SQL query and the rest of the work is done in-memory. I understand that there's no way for the EF team to account for every possible expression that a developer may come up with and magically translate that into a useable SQL query, but IIRC, EF would throw an exception if it was unable to translate ALL of the operations defined in an IQueryable to SQL. Is there a way to

Net Core: Generic Repository Primary Id Key Performance in Entity Framework

六眼飞鱼酱① 提交于 2019-12-02 00:22:53
问题 We are reviewing two different methods in generic repository patterns. Currently, want to map primary keys to Ids. The purpose of this is to map to the Generic Repository Interface which utilizes Id. Two solutions are provided below. What are performance implications of .FindPrimaryKey().Properties. Does it cause a schema lock on database table in trying to find the primary key? Does it cause any application slowness? How does it compare in performance vs Partial Class Method Solution 2? What

Multi dimensional relationship looses the relation with wrapper

走远了吗. 提交于 2019-12-02 00:12:48
I think it's something wrong with the model in the example below, but I can't figure out what. In the example I have a Container class with Items containing sub Items. As soon as I try to create a container with more than one level of Items it looses the relationship with the container and therefore fails due to foreign key constraints. The exception I get is: Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details. ---- System.Data.SqlClient.SqlException : The INSERT statement conflicted with the FOREIGN KEY

How do you add the equivalent of the OwnedAttribute using FluentAPI?

让人想犯罪 __ 提交于 2019-12-01 23:26:08
问题 I can't seem to do this without using either the attribute on TrackState or specifying OwnsOne for Publishers + Articles. Is there any way i can globally mark TrackState as an owned type without using the attribute? (for people comming through google: How do you add attributes to entities using fluent api?) (Entities + EF core are in seperate libraries and i do not want a dependency there on EF) public class Publisher { public int Id { get; set; } public string Name { get; set; } public

Optional Parameters with EF Core FromSql

房东的猫 提交于 2019-12-01 23:13:43
Is it possible to use EF Core FromSql to execute a stored procedure that has optional parameters? I have been testing out a simple scenario to use as a template for updating old EF6 calls to EF Core calls. The test example I am using as a proof of concept: CREATE PROCEDURE [dbo].[TestNullableParameters] @addressId int = null, @city nvarchar(100) = null, @createdByUserId int AS BEGIN select * from CRM..Address a where (@addressId is null or a.AddressId = @addressId) and (@city is null or a.City like @city) and a.CreatedByUserId = @createdByUserId END My test code that calls this proc: [Test]

Getting DbContext when resolving a singleton

喜你入骨 提交于 2019-12-01 22:57:33
问题 Within ConfigureServices I have services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); as well as services.AddSingleton<IMyModel>(s => { var dbContext = s.GetService<MyContext>(); var lastItem= dbContext.Items.LastOrDefault(); return new MyModel(lastItem); }); But s.GetService<MyContext>() throws an error: Cannot resolve scoped service 'MyContext' from root provider. How can I achieve that? I don't want to inject MyDbContext