entity-framework-core

Entity Framework Core Stored Proc without DbSet

前提是你 提交于 2019-12-06 08:04:08
问题 I am calling a stored proc from EF Core 1.1, following the advice on https://docs.microsoft.com/en-us/ef/core/querying/raw-sql But I created a class specifically that matches the shape of data returned from the stored proc List<MyStoredProcResultType> results = context.MyStoredProcResultType .FromSql("EXECUTE dbo.MyStoredProc {0}", someParam) .ToList(); But this means I have to create a DbSet for MyStoredProcResultType in the Context and now it is legal to code this context

Change the connection string dynamically (per request) on Entity Framework 7 / MVC 6

一笑奈何 提交于 2019-12-06 07:09:54
I have a MVC 6 application on which I need to connect to a different database (i.e. physical file but same schema) depending on who is accessing to it. That is: each customer of the web application will have it's data isolated in an SQL database (on Azure, with different performances, price levels, etc.) but all those databases will share the same relational schema and of course, the Entity Framework context class. var cadConexion = @"Server=(localdb)\mssqllocaldb;Database=DBforCustomer1;Trusted_Connection=True;"; services.AddEntityFramework().AddSqlServer().AddDbContext<DAL.ContextoBD>

.net core - unit of work generic repository pattern

扶醉桌前 提交于 2019-12-06 06:41:12
问题 I am Looking to migrate a large project that gathers information about products. The current application uses CSLA as its framework (traditional aspx forms). I am wanting to migrate / develop the application over to .net core 2.1. I have experience of developing in MVC (4+ years) and some recent exposure to .net core. I am wanting to use EF Core to call the existing stored procedures. I am looking at using the Unit of Work Generic repository design pattern. I have used the repository pattern

Pattern for caching data in Asp.Net Core + EF Core?

跟風遠走 提交于 2019-12-06 06:04:46
问题 I have an Asp.Net Core + EF Core REST service. I created a DbContext class for a DB I want to call a SP on. The method pretty much looks like: public IQueryable<xxx> Getxxxs() { return Set<xxx>().FromSql("pr_Getxxx"); } This all works, but there isn't any point in calling the SP every single time since the data the SP returns rarely changes. I'd like to make the data stale, say every 24 hours. Is there a preferred pattern for doing that in Core? I see they have the .AddCaching extension

Is it possible to intercept a READ action?

删除回忆录丶 提交于 2019-12-06 05:54:39
问题 In Entity Framework Core, you can override SaveChanges / SaveChangesAsync method in the DbContext and based on different states like: EntityState.Added or EntityState.Modified or EntityState.Deleted , you can create some Audit solution about when and whom created, modified or deleted certain records. You can save the state of the entity before and after the action. All good here works perfect! Can we do something similar for read/query/select/view actions? 回答1: I dug a little and found that

EF: The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked

只愿长相守 提交于 2019-12-06 05:39:57
问题 I am sending a User entity in Json format in my http request like this: POST http://localhost:52054/api/Authentication/DeleteAccessToken HTTP/1.1 Host: localhost:52054 Content-Type: application/json {"id":1,"userName":"mnoureldin","accessToken":{"id":1,"token":"123ABC456EFG","userId":1}} And my controller (in EF-core ) handles that like this: [HttpPost] public IActionResult DeleteAccessToken([FromBody]User user) { using (var Context = new UnitOfWork().Context) { var userEntity = Context.Users

EF Core: Using ID as Primary key and foreign key at same time

点点圈 提交于 2019-12-06 05:36:10
I have two entites, Prospect and person, what I'm trying to do is use Prospect.ID as primary key on ProspectTable and as foreignkey of PersonID, my ideia is use the same ID for both entities without the need of a PersonID on my Prospect entity. When the prospect is being saved on database, it tries to save a PersonID even I not have this property on my Prospect entity, I would like to know if ef core supports this kind of relationship. Here's what I got on my model builder. modelBuilder.Entity<ProspectDto>(builder => { builder.ToTable("Prospects"); builder.HasKey(prospect => prospect.ID); });

Entity Framework Core: What is the fastest way to check if a generic entity is already in the dbset and then add or update it?

白昼怎懂夜的黑 提交于 2019-12-06 04:44:50
I load data from a CSV file, I create entities on the fly then I add them to a List<T> . The following code takes a List<T> and adds the entities to the right DbSet . public static void AddEntities<T>(List<T> entities, DbContext db) where T :class { using (db) { var set = db.Set<T>(); foreach(T e in entities) { set.Add(e); } db.SaveChanges(); } } I'd like to change it to add the entity only if it doesn't exist, otherwise it has to update it. What is the best way to accomplish this with Entity Framework Core? I believe I should use System.Reflection to: get at least the ID but it would be

what is the difference between use dbset and mappers in EF7

家住魔仙堡 提交于 2019-12-06 04:39:16
问题 I started to work with .net core and Entityframework 7 in Onion Architecture ! i readed this tutorial and i think its best case for learning following subject. but one part of this tutorial made a big question inside my brain. like what you see at this linked page; at Data Layer we have some classes which are our model!! public class User : BaseEntity { public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public virtual UserProfile

dotnet exec needs a managed .dll or .exe extension while adding Entity Framework Core (1.1.0) Migrations

北城余情 提交于 2019-12-06 04:24:54
Error Message: PM> Add-Migration InitialDatabase dotnet exec needs a managed .dll or .exe extension. The application specified was 'C:\Users\xxxxxx\documents\visual studio 2017\Projects\TheWorld\src\TheWorld\bin\Debug\netcoreapp1.0\TheWorld.runtimeconfig.json' Process finished with non-zero exit code PM> Visual Studio Version: 2017 RC Project Dependencies : Error Screenshot : I had the same problem. The only thing I had to do is changing the Target Framework in the Project properties. Changing the framework version I hope this helps. Shiju Narayan In VS2017RC - All I had to do was install