entity-framework-core

EF Core There is already an object named 'AspNetRoles' in the database

霸气de小男生 提交于 2019-12-24 03:36:08
问题 I am receiving There is already an object named AspNetRoles in the database. error while attempting to use update-database command in NuGet console. Context: public class ApplicationDbContext : IdentityDbContext<IdentityUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Customer> Customers { get; set; } } Startup: public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public

How to create “inline if statement” with expressions in dynamic select for null checking

两盒软妹~` 提交于 2019-12-24 03:31:03
问题 How to create "inline if statement" with expressions in dynamic select for null checking I was wrote a dynamic linq select expression for a nested property of a object, but it throw an exception when it is null. so i want to check whether that property is null or not, that simple ! here is what i mean: X.Where(...) .Select(X => new Y{ ... Z = X.Titles == null ? "" : [Linq] ... }).FirstOrDefault(); here is what i wrote private static Expression GetLocalizedString(Expression stringExpression,

How to create “inline if statement” with expressions in dynamic select for null checking

£可爱£侵袭症+ 提交于 2019-12-24 03:30:03
问题 How to create "inline if statement" with expressions in dynamic select for null checking I was wrote a dynamic linq select expression for a nested property of a object, but it throw an exception when it is null. so i want to check whether that property is null or not, that simple ! here is what i mean: X.Where(...) .Select(X => new Y{ ... Z = X.Titles == null ? "" : [Linq] ... }).FirstOrDefault(); here is what i wrote private static Expression GetLocalizedString(Expression stringExpression,

Why am I getting a contentRootPath null exception when attempting to add a database migration in entityframework core?

ぐ巨炮叔叔 提交于 2019-12-24 02:38:11
问题 I have a .NET Core class library that is the data access layer for multiple web applications. I am using Entity Framework Core 1.1, but I'm having trouble with migrations through PMC. My initial migration worked successfully, but after updating the database and re-launching VS 2017 I get a null exception on the contentRootPath argument when attempting to migrate. I may have made some changes to the project after my successful migration, but I don't remember what they were. This is the error

Entity framework core: The property expression 'xx' is not valid. The expression should represent a property access: 't => t.MyProperty'

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:14:05
问题 Trying to query with where clause at the leaf level in entity framework core but getting this error: Unhandled Exception: System.ArgumentException: The property expression 'c => {from Child t in c.Children where ([t].Name != "NoInc lude") select [t]}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393. at Microsoft.EntityFrameworkCore.Internal

EF Core 2.0 provider specific model configuration

孤街醉人 提交于 2019-12-24 01:53:58
问题 I am trying to upgrade an EF Core 1.x project to 2.0. In 1.x I was taking advantage of provider-specific extension methods (e.g. ForSqlServerHasDefaultValueSql / ForSqliteHasDefaultValueSql or ForSqlServerHasColumnType ) but these seem to have been removed in 2.0. Since each provider has its own flavor of SQL and capabilities, I need to be able to use slightly different setups or SQL strings per provider in OnModelCreating . For example to set a default value I might have: modelBuilder<Foo>(b

How to specify default property values for owned entity types in Entity Framework Core 2.0?

夙愿已清 提交于 2019-12-24 01:36:12
问题 I have a simple POCO type, say something like public class OwnedEntity { public string stringProperty { get; set; } public decimal decimalProperty { get; set; } public bool boolProperty { get; set; } public int intProperty { get; set; } } and an actual entity with an OwnedEntity reference public class SomeEntity { public string Id { get; set; } public OwnedEntity OwnedEntity { get; set; } } I set up the relationship like described in the documentation using EF Core's Fluent API: protected

Enable EF Core Migration in Full .NET Project (*.csproj)

筅森魡賤 提交于 2019-12-24 01:15:02
问题 I have a full .NET Framework project (*.csproj) with EF Core data access. How can I have migrations enabled for EF Core in Non-.NET-Core projects (ASP.NET MVC5)? I get the following error: dotnet : No executable found matching command "dotnet-ef" 回答1: The 1.0.0-preview2 and 1.0.0-preview1 releases of EF Core Tools don't work on csproj projects, only project.json. Use the Package Manager Console commands instead. See https://docs.efproject.net/en/latest/miscellaneous/cli/powershell.html 来源:

Entity Framework Core linq query returns InvalidCastException

 ̄綄美尐妖づ 提交于 2019-12-24 00:59:03
问题 I am currently creating a web api in .net CORE with Entity Framework. Whenever I try to select a single record by ID with linq, I get the following error: An exception of type 'System.InvalidCastException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code. Additional information: Unable to cast object of type 'System.Int32' to type 'System.Char'. This error happens whenever I execute the following linq query: int id = 1; User user = context.Users.First(i => i

.NET Core EF, cleaning up SqlConnection.CreateCommand

柔情痞子 提交于 2019-12-24 00:48:26
问题 I am using .NET Core DI to get DbContext and in my logic I need to execute raw SQL commands also on DB, so for that purpose I am creating DbCommand to execute SQL like this(just an example query, actual one is little complex so not writing here for simplicity): public string GetId() { var cmd = _context.Database.GetDbConnection().CreateCommand(); bool isOpen = cmd.Connection.State == ConnectionState.Open; if (!isOpen) { cmd.Connection.Open(); } cmd.CommandText = "Select TOP 1 ID from ABC;";