entity-framework-core

EF Core with ASP MVC .NET 4.6

橙三吉。 提交于 2020-01-13 09:17:06
问题 In a project I need to set up an ASP.NET MVC (with .NET 4.6.1) but using the "new" EF Core for accessing the database. Unfortunately every documentation explains only how to setup an ASP.NET Core MVC project. I just gave it a try and when it comes to creating the database via the Package Manager Console I get the error message: No parameterless constructor was found on 'DataContext'. Either add a parameterless constructor to 'DataContext' or add an implementation of 'IDbContextFactory' in the

How to discard changes to context in EF Core

不羁的心 提交于 2020-01-13 08:29:20
问题 I have a huge list of "flattened" objects in json format, and a somewhat complicated relational DB schema (with about 20 tables corresponding to a flattened object). I'm trying to automate the insertions of those flattened objects in my new relational database: foreach (var flattenedObject in flattenedObjects) { _repository.Insert(flattenedObject).Wait(); //some time logging, etc } The Insert() method calls AddRangeAsync() and AddAsync() for a number of related objects in different tables.

Entity Framwork 7 reverse engineer from existing db “code first”

主宰稳场 提交于 2020-01-13 05:49:28
问题 I can't figure out how to pass the connection string parameter in the latest version of dnx... trying dnx ef dbcontext scaffold -connection='sql connection string' but it doesn't recognize the -connection parameter .. how are you supposed to pass arguments into the thing? 回答1: Have you tried: dnx ef dbcontext scaffold "sql connection string" EntityFramework.MicrosoftSqlServer 来源: https://stackoverflow.com/questions/32377984/entity-framwork-7-reverse-engineer-from-existing-db-code-first

Set Value before save

社会主义新天地 提交于 2020-01-13 04:26:28
问题 I was wondering if there is any way to set a value to an entity onsave? Because I'm working on a multi tenant web application and I would like to set the the current tenant ID (through simple DI service). I tried using HasDefaultValue() in Fluent API, however this will try to convert to a SQL function. So this doesn't work for me. builder.Entity<Order>( ) .HasQueryFilter(p => p.TenantId == _tenantProvider.GetTenantId()) .Property(p => p.TenantId) .HasDefaultValue(_tenantProvider.GetTenantId()

Entity Framework Core - How to check if database exists?

淺唱寂寞╮ 提交于 2020-01-12 06:56:11
问题 For EF6, I can check whether a database exists in the following way: context.Database.Exists() How can I do this in EF Core? 回答1: I have found the solution on my own: (context.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists() It works for EF 7.0.0-rc1-final version for SqlServer UPDATE: Entity Framework Core 2.0: (context.Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists() 回答2: Probably too late but what about context.Database.EnsureCreated() ?

MongoDB and Entity Framework Core 2.0

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-12 05:12:26
问题 So, I've read repeatedly that EF Core will support NoSQL databases but I can't seem to find any "official" NoSQL database providers, or even a NoSQL framework in the source code. (By "find" I mean search for "nosql".) I've had a quick look at ADO.NET (paid) and crhairr/EntityFrameworkCore.MongoDb but they are both third-party. MongoDB (the specific database I was looking into) has their own .NET driver but it doesn't seem to integrate EF Core. Anyway, what I really want to know is: Does/will

Column encryption in ASP MVC app with SQL Server 2016 using .net Core / EF Core

别来无恙 提交于 2020-01-12 03:31:06
问题 I am trying to use the "Always Encrypted" feature in SQL Server 2016 to encrypt some columns. I used this post as a guide to set the columns as encrypted in SSDT. That part goes fine, it's when I attempt to query the data from the application that I get an error. According to the docs I need to add this: column encryption setting=enabled to my connection string. This does not appear to be supported in Entity Framework Core. I get this error: column encryption setting=enabled is not supported

Add foreign key to AspNetUser table

跟風遠走 提交于 2020-01-11 14:48:12
问题 I've created an ASP.NET Core Razor Pages application with user identities. The created project includes a migration that adds all the user tables, such as AspNetUser and AspNetRoles . However, it doesn't create any code models for these tables. Now I've created my own entity models and I would like some of them to have foreign keys to the AspNetUser table. How can I do this? Does the platform expose these as classes anyplace? Do I need to manually create models and try to get them to exactly

Get generated SQL for a DbContext.SaveChanges in Entity Framework Core

对着背影说爱祢 提交于 2020-01-11 11:56:48
问题 In Entity Framework Core, is it possible to see the SQL that will be applied when the SaveChanges() method is called on the DbContext ? 回答1: you can use console logger "EF Core logging automatically integrates with the logging mechanisms of .NET Core " you can read about here : https://www.entityframeworktutorial.net/efcore/logging-in-entityframework-core.aspx 回答2: You can use DbContextOptionsBuilder.UseLoggerFactory(loggerFactory) method to log all sql output.By using constructor Injection

Entity Framework Core Inheritance creating child tables

断了今生、忘了曾经 提交于 2020-01-11 08:45:29
问题 public class Product { public string Name { get; set; } public int Qty { get; set; } public decimal Price { get; set; }`` } public class CartItem : Product { public int CartItemId { get; set; } public string CartId { get; set; } } public class OrderLine : Product { public int OrderLineId { get; set; } public int OrderId { get; set; } } public class Kititem : Product { public int KititemId { get; set; } public int OrderId { get; set; } } public class SampleContext : DbContext { public DbSet