entity-framework-core

Best practice calling scalar functions with Entity Framework Core (2.1)

青春壹個敷衍的年華 提交于 2019-12-21 03:52:27
问题 I often need to call scalar functions that are defined on a SQL Server from my web applications (ASP.NET Core / EF Core). Since these functions are just simple helper functions and I also use a lot of them I use a general pattern for calling these scalar functions - with the help of the new query types available from EF Core 2.1. Since I am relatively new to EF Core my question is if this pattern might cause problems and/or if there is a better solution or best practice for calling scalar

How to execute SqlQuery with Entity Framework Core 2.1?

房东的猫 提交于 2019-12-21 03:37:07
问题 In Entity Framework 6, I can execute a raw SQL query on the database using the following command: IEnumerable<string> Contact.Database.SqlQuery<string>("SELECT a.title FROM a JOIN b ON b.Id = a.aId WHERE b.Status = 10"); On a new project, I am trying to use Entity Framework Core 2.1. I have a need to execute raw SQL query. While googling, I can see that the extension SqlQuery was changed to FromSql . However, FromSql only exists on the DbSet<> not on the DbContext.Database . How can I run

Entity Framework Core migration - connection string

為{幸葍}努か 提交于 2019-12-21 03:20:53
问题 I'm having a problem to handle the DB connection string in conjunction with migrations. I have 2 projects: Domain Application The DbContext is in the Domain project, so this is the project I run migrations against. The migrations concept enforces me to implement OnConfiguring in my DbContext and therein specify the database provider, eg: protected override void OnConfiguring(DbContextOptionsBuilder builder) { builder.UseSqlServer("<connection string>"); } My problem is that I don't want to

Generic method for updating EFCore joins

左心房为你撑大大i 提交于 2019-12-21 02:53:06
问题 One thing that I am finding really tedious with the way EFCore handles many-to-many relationships is updating an entities joined collections. It is a frequent requirement that a viewmodel comes from frontend with a new list of nested entities and I have to write a method for each nested entity that works out what needs to be removed, what needs to be added and then do the removes and adds. Sometimes an entity has multiple many-to-many relationships and I have to write out pretty much the same

Rename a foreign key in Entity Framework Core without dropping data

北城以北 提交于 2019-12-21 02:35:35
问题 I have two model classes: public class Survey { public int SurveyId { get; set; } public string Name { get; set; } } public class User { public int UserId { get; set; } public int SurveyId { get; set; } public Survey Survey { get; set; } } I want to rename Survey to StudentSurvey , so it will have StudentSurveyId . I update the class name and the properties in the models accordingly and add a migration. However, I get: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK

Where is the dotnet command executable located on Windows?

僤鯓⒐⒋嵵緔 提交于 2019-12-20 18:31:46
问题 I am exploring the new Entity Framework Core (NOT in conjunction with ASP.Net, what I am coding is just a WinForms app) and found some tutorials mentioning a dotnet command line command needed to create "migrations". When I try it, however, it says 'dotnet' is not recognized as an internal or external command, operable program or batch file. I have searched my hard drive for "dotnet.exe", "dotnet.bat" and "dotnet.cmd" but have found nothing. I use Visual Studio 2015 Community Edition. Where

EF Core Connection to Azure SQL with Managed Identity

房东的猫 提交于 2019-12-20 17:34:53
问题 I am using EF Core to connect to a Azure SQL Database deployed to Azure App Services. I am using an access token (obtained via the Managed Identities) to connect to Azure SQL database. Here is how I am doing that: Startup.cs: public void ConfigureServices(IServiceCollection services) { //code ignored for simplicity services.AddDbContext<MyCustomDBContext>(); services.AddTransient<IDBAuthTokenService, AzureSqlAuthTokenService>(); } MyCustomDBContext.cs public partial class MyCustomDBContext :

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

旧街凉风 提交于 2019-12-20 14:36:49
问题 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? 回答1: 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

Generating and accessing stored procedures using Entity framework core

╄→尐↘猪︶ㄣ 提交于 2019-12-20 12:08:50
问题 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

Saving many-to-many relationship in Entity Framework Core

时光怂恿深爱的人放手 提交于 2019-12-20 08:39:42
问题 For example, I have 3 classes, which I'm using for many-to-many relationship: public class Library { [Key] public string LibraryId { get; set; } public List<Library2Book> Library2Books { get; set; } } public class Book { [Key] public string BookId { get; set; } public List<Library2Book> Library2Books { get; set; } } public class Library2Book { public string BookId { get; set; } public Book Book { get; set; } public string LibraryId { get; set; } public Library Library { get; set; } } They're