ef-core-2.1

EF Core: Use Linked Server to Oracle database issue

允我心安 提交于 2019-12-25 00:06:08
问题 In another question, I need to use ASPNET BoilerPlate (EF Core template) with the Oracle database but it does not support so far. So I tried to use SQL Server with Linked Server to Oracle database. What I have tried: Create Linked Server named "LinkedOracle" from SqlServer to Oracle database Create a SqlServer database named "FakeOracleDb" and create Views to LinkedOracle tables by: CREATE VIEW CUSTOMER AS SELECT * FROM LinkedOracle..MYUSER.CUSTOMER Now I have a fake SQL Server database that

Scaffold-DbContext not generating virtual navigation properties (EF Core 2.1)

不打扰是莪最后的温柔 提交于 2019-12-24 00:05:29
问题 I am using EF Core 2.1 database-first. I use Scaffold-DbContext to create/update my models (the .cs files) I want to enable lazy-loading, and I have added .UseLazyLoadingProxies() in my Startup.cs Now, how do I get Scaffold-DbContext to generate virtual navigation properties? 来源: https://stackoverflow.com/questions/50827976/scaffold-dbcontext-not-generating-virtual-navigation-properties-ef-core-2-1

Pass current transaction to DbCommand

我是研究僧i 提交于 2019-12-23 08:50:00
问题 I'm working on a project using ASP.NET Core 2.1 and EF Core 2.1. Although most of queries and commands use EF, some units needs to call stored procedures directly. I can't use FromSql , because it needs results set based on entity models. Let's assume we have these methods: public Task CommandOne(DbContext context) { Entity entity = new Entity { Name = "Name" }; context.DbSet<Entity>().Add(entity); return context.SaveChangesAsync(); } public async Task CommandTwo(DbContext context) {

Entity Framework Core 2.1 - Multiple Providers

不羁岁月 提交于 2019-12-22 09:52:54
问题 What is the right way to work with multiple providers? My Example: appsettings.json { "ConnectionStrings": { "Sqlite": "Data Source=database.db" } } Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddDbContext<DatabaseContext>(options => options.UseSqlite(Configuration.GetConnectionString("Sqlite"))); } DatabaseContext.cs public class DatabaseContext : DbContext { public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } public

How to get COUNT DISTINCT in translated SQL with EF Core

北慕城南 提交于 2019-12-21 15:15:07
问题 I want to have EF core translate .Select(x=>x.property).Distinct().Count() into something like SELECT COUNT(DISTINCT property) Let's take an example. Let's say I have a DB table with PersonID(long), VisitStart(datetime2) and VisitEnd(datetime2). If i want to get the number of distinct days a particular person has visited, then I could write SQL like SELECT COUNT(DISTINCT CONVERT(date, VisitStart)) FROM myTable GROUP BY PersonID But using EF core and this MyTable .GroupBy(x=>x.PersonID)

How to get COUNT DISTINCT in translated SQL with EF Core

隐身守侯 提交于 2019-12-21 15:14:12
问题 I want to have EF core translate .Select(x=>x.property).Distinct().Count() into something like SELECT COUNT(DISTINCT property) Let's take an example. Let's say I have a DB table with PersonID(long), VisitStart(datetime2) and VisitEnd(datetime2). If i want to get the number of distinct days a particular person has visited, then I could write SQL like SELECT COUNT(DISTINCT CONVERT(date, VisitStart)) FROM myTable GROUP BY PersonID But using EF core and this MyTable .GroupBy(x=>x.PersonID)

Optional Parameters with EF Core FromSql

倖福魔咒の 提交于 2019-12-20 03:43:11
问题 Is it possible to use EF Core FromSql to execute a stored procedure that has optional parameters? I have been testing out a simple scenario to use as a template for updating old EF6 calls to EF Core calls. The test example I am using as a proof of concept: CREATE PROCEDURE [dbo].[TestNullableParameters] @addressId int = null, @city nvarchar(100) = null, @createdByUserId int AS BEGIN select * from CRM..Address a where (@addressId is null or a.AddressId = @addressId) and (@city is null or a

How to seed an Admin user in EF Core 2.1.0?

我怕爱的太早我们不能终老 提交于 2019-12-17 18:27:38
问题 I have an ASP.NET Core 2.1.0 application using EF Core 2.1.0. How do I go about seeding the database with Admin user and give him/her an Admin role? I cannot find any documentation on this. 回答1: As user cannot be seeded in a normal way in Identity just like other tables are seeded using .HasData() of .NET Core 2.1. Seed Roles in .NET Core 2.1 using code given below in ApplicationDbContext Class : protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating

Entity Framework Core 2.1, rename table using code-first migrations without dropping and creating new table

大兔子大兔子 提交于 2019-12-13 13:12:54
问题 I'm using netcoreapp 2.1 with EF Core 2.1 and updating my database with data migrations and have come into a problem with renaming tables. My main issue here is a table (and later potentially columns) may be renamed multiple times with data and I want to be able to rename them while keeping this data intact but from what I've read it seems these migrations only seem concerned with keeping the schema up to date. This issue is similar to Change or rename a column name without losing data with

Is there anyway to execute this Expression and get the actual value not as object?

青春壹個敷衍的年華 提交于 2019-12-13 03:45:00
问题 Given an Expression that returns a IQueryable<sometype> . If I don't know what sometype is at compile-time. Can I somehow execute the Expression and get IQueryable<actual type> returned. Obviously I can use, Expression.Lambda<Func<object>>(expressionInstance).Compile()() But of course I'll get an object back. Or, I can use, Expression.Lambda<Func<IQueryable>>(expressionInstance).Compile()() But I'll get back a IQueryable which is of no use if I want to actually use any extension methods such