entity-framework-core

Entity Framework Core - setting the decimal precision and scale to all decimal properties [duplicate]

ε祈祈猫儿з 提交于 2019-12-03 04:46:04
问题 This question already has an answer here : Loop/reflect through all properties in all EF Models to set Column Type (1 answer) Closed 2 years ago . I want to set the precision of all the decimal properties to (18,6). In EF6 this was quite easy: modelBuilder.Properties<decimal>().Configure(x => x.HasPrecision(18, 6)); but I can't seem to find anything similar to this in EF Core. Removing the cascade delete convention wasn't as simple as in EF6 so I found the following workaround: EF6:

How do I write unit tests for ASP.NET Core controllers that actually use my Database Context?

眉间皱痕 提交于 2019-12-03 04:41:27
There seems to be little information about how to write good unit tests for actual ASP.NET Core controller actions. Any guidance about how to make this work for real? I've got a system that seems to be working pretty well right now, so I thought I'd share it and see if it doesn't help someone else out. There's a really useful article in the Entity Framework documentation that points the way. But here's how I incorporated it into an actual working application. 1. Create an ASP.NET Core Web App in your solution There are tons of great articles out there to help you get started. The documentation

Entity Framework core : DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

自古美人都是妖i 提交于 2019-12-03 04:41:05
问题 I'm new to EF core and I'm trying to get it to work with my asp.net core project. I get the above error in my startup.cs when trying confiugure the dbcontext to use a connection string from config. I'm following this tutorial : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro The problematic code in startup.cs : using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting;

Generating and accessing stored procedures using Entity framework core

痴心易碎 提交于 2019-12-03 02:28:19
I am implementing Asp.Net core Web API , entity framework core, database first approach using Visual Studio 2017. I have managed to generate the context and class files based on an existing database. I need to access stored procedures using my context. In earlier version of entity framework it was simple by selecting the stored procedure objects in the wizard and generating an edmx that contains those objects. I could then access stored procedures via the complex type objects exposed by entity framework. How do I do a similar thing in entity framework core. An example would help ? Database

How can I reset an EF7 InMemory provider between unit tests?

孤街浪徒 提交于 2019-12-03 00:54:32
I am trying to use the EF7 InMemory provider for unit tests but the persistent nature of the InMemory database between tests is causing me problems. The following code demonstrates my issue. One test will work and the other test will always fail. Even though I set the _context to null between tests the second test run will always have 4 records in it. [TestClass] public class UnitTest1 { private SchoolContext _context; [TestInitialize] public void Setup() { Random rng = new Random(); var optionsBuilder = new DbContextOptionsBuilder<SchoolContext>(); optionsBuilder.UseInMemoryDatabase();

Entity Framework Core: How to get the Connection from the DbContext?

你说的曾经没有我的故事 提交于 2019-12-02 22:18:04
I am trying the new Entity Framework Core with MySQL Connector. I can get a valid DbContext and write into the database so everything has been setup correctly. I need to get the Connection from the DbContext because I have to test for it at application starting using a connection.Open() inside a try statement. If there is not a valid connection, the console app should try to start MySQL Server and retry. How can I get the Connection from the DbContext ? Before EF6 by context.Connection . After EF6 by context.Database.Connection . It seems the latest has been removed too from EFCore. The

ERR_CONNECTION_RESET returning Async including object child collections

南楼画角 提交于 2019-12-02 19:27:36
问题 I am building a net core stack and implementing the layers as async. I have the following customer repository code that is called by a web api layer public class CustomerRepository : ICustomerRepository { //interface this up private DbContextOptionsBuilder<SykesTestContext> optionsBuilder = new DbContextOptionsBuilder<SykesTestContext>(); public CustomerRepository(string connection) { optionsBuilder.UseSqlServer(connection); } public async Task<List<Dom.Customer>> GetAll() { List<Dom.Customer

Entity Framework core : DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

血红的双手。 提交于 2019-12-02 18:40:56
I'm new to EF core and I'm trying to get it to work with my asp.net core project. I get the above error in my startup.cs when trying confiugure the dbcontext to use a connection string from config. I'm following this tutorial : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro The problematic code in startup.cs : using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.SpaServices.Webpack; using Microsoft.Extensions.Configuration; using

Entity Framework Core add unique constraint code-first

假如想象 提交于 2019-12-02 18:17:53
I can't find way to add a unique constraint to my field with using attribute: public class User { [Required] public int Id { get; set; } [Required] // [Index("IX_FirstAndSecond", 2, IsUnique = true)] not supported by core public string Email { get; set; } [Required] public string Password { get; set; } } I'm using these packages: "Microsoft.EntityFrameworkCore": "1.0.1", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", On EF core you cannot create Indexes using data

Can I stop Entity Framework Core from populating my result with partial data?

痞子三分冷 提交于 2019-12-02 17:13:05
问题 On this page of the Entity Framework Core documentation, it says when querying loaded data: Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded. This is true whether it is Eager or Explicit . I find this to be frustrating,