entity-framework-core

Transaction Per Request Middleware Not working

南楼画角 提交于 2019-12-12 01:19:26
问题 I am trying to add a middleware so that transaction begins at the beginning of the request and commits or rollbacks depending on the situation. This is my middleware: public class TransactionPerRequestMiddleware { private readonly RequestDelegate next_; public TransactionPerRequestMiddleware(RequestDelegate next) { next_ = next; } public async Task Invoke(HttpContext context, AppDbContext dbContext) { var is_everything_ok = true; var transaction = dbContext.Database.BeginTransaction( System

MissingMetadataException when building UWP app with .Net native

你说的曾经没有我的故事 提交于 2019-12-11 23:27:50
问题 I have this UWP app that uses a project (UWP class library) which itself uses EF7 and SQLite. I tried to build the app in the Release mode using .Net native tool chain, the build completes successfully (after a good long time, and after eating as much memory as it can), but the application crashes just after leaving the splash screen. After following some advice on SO I tried the .Net native with Debug mode, the build finishes just like in the Release mode, but I get many errors on the output

How to remove child entities when saving attached entity with EF Core

て烟熏妆下的殇ゞ 提交于 2019-12-11 22:59:56
问题 I have a Repository using EF Core. In the below scenario I first fetch an entity along with a related child collection. I then remove all the children and add a new child and also update one property on the parent entity. After saving the property on the parent is update, the new child entity is added but all the original child entities are still in the database. How can I get EF to track the removed child entities as deleted? class Repository { public Parent GetParent(int id) { using (var db

Getting sqlite to work in ASP.NET 5 (vnext)

淺唱寂寞╮ 提交于 2019-12-11 20:40:18
问题 I'm trying to get "EntityFramework. SQLite ": "7.0.0-beta1" to work within ASP.NET 5. I've created my project using Yeoman and installed EF sqlite from the package manager. The project builds fine but when running I get: Could not load file or assembly 'Microsoft.Framework.Logging.ILogger, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'or one of its dependencies. My project.json: "dependencies": { "Microsoft.AspNet.Diagnostics": "1.0.0-beta5", "Microsoft.AspNet.Mvc": "6.0.0-beta5",

Building filters on DbContext

青春壹個敷衍的年華 提交于 2019-12-11 19:17:29
问题 I have an ASP .Net Core 2.1 Web Api. I was wondering if there is an "elegant solution" for constructing filters on my DbContext based on query strings? So... say I have one [optional] query string: // GET: api/Accounts [HttpGet] public IEnumerable<File> GetAccount([FromQuery] bool? isActive) { if (isActive.HasValue) return _context.Accounts.Where(a => a.IsActive == isActive.Value); else return _context.Accounts; } Simple enough... But say I have a number of (optional) query strings: // GET:

Entity Framework core select causes too many queries

淺唱寂寞╮ 提交于 2019-12-11 18:38:18
问题 I have the following method which is meant to build me up a single object instance, where its properties are built via recursively calling the same method: public ChannelObjectModel GetChannelObject(Guid id, Guid crmId) { var result = (from channelObject in _channelObjectRepository.Get(x => x.Id == id) select new ChannelObjectModel { Id = channelObject.Id, Name = channelObject.Name, ChannelId = channelObject.ChannelId, ParentObjectId = channelObject.ParentObjectId, TypeId = channelObject

Target .NET4.6 or .NET Core 1 for greenfield development?

空扰寡人 提交于 2019-12-11 18:36:47
问题 I'm starting to understand the naming conventions for the .NET stack cross-platform. Particularly this page explained it well: http://blog.tonysneed.com/2016/01/22/ef6-asp-net-core-mvc6/ I'm starting a personal project which will be completely greenfield and see it as an opportunity to learn all these new technologies. However, I want to re-use code from other MVC5 applications that use well known libraries. Especially for the infrastructure & plumbing I used some well-known libraries such as

IQueryable<T> from raw SQL

不问归期 提交于 2019-12-11 18:32:32
问题 In Entity Framework 7, there is DbSet.FromSql , which seems to me that allows to create IQueryable<> from raw SQL. According to the doc: Raw SQL queries provides the DbSet.FromSql method to use raw SQL queries to fetch data. These queries can also be composed on using LINQ. How to implement something similar in old LINQ to SQL? 来源: https://stackoverflow.com/questions/32144330/iqueryablet-from-raw-sql

How can I reset the ConnectionString of EntityFramework .NET Core WebServer

社会主义新天地 提交于 2019-12-11 17:58:27
问题 A common setup with Docker: Two linux containers, one a .NET Core WebServer using EntityFramework Core 2.2.6, the other a MS-SQLServer 2017. Persistent data is being held in a Docker volume. Using docker-compose , it's not a swarm. When starting the SQLServer container, one must provide the SA password as an environment variable to the container. However you provide that, it is possible to later read this env from outside the container using docker container inspect . Which obviously

How do I deal with iterating over a one to many (or zero) entity when there are zero/null entry?

梦想与她 提交于 2019-12-11 17:55:26
问题 I keep getting NullReferenceException trying to iterate over a null entry on a row. public class Person { public int ID { get; set; } public string Name { get; set; } public Address Address { get; set; } public int AddressID { get; set; } } public class Address { public int ID { get; set; } public string FirstLine { get; set; } public string SecondLine { get; set; } } With the default scaffold template, _context.Person.Include(c => c.Address); The view works fine if there is an address or not