ef-core-2.1

Seed entity with owned property

时光怂恿深爱的人放手 提交于 2020-02-14 02:45:27
问题 I am trying to seed an user entity in my database. The User entity has an owend property EmailPermissions . When I run the command dotnet ef migrations add Initial; I get the error The seed entity for entity type 'User' cannot be added because it has the navigation 'EmailPermissions' set. To seed relationships you need to add the related entity seed to 'EmailPermissions' and specify the foreign key values {'UserId'}. but since EmailPermissions is an owned entity I didn't give it an explicit

EF Core - How can I retrieve associated entities of an aggregate root?

吃可爱长大的小学妹 提交于 2020-01-05 04:27:12
问题 In my app I am trying to follow DDD whilst modelling a simple fund account. The classes are FundAccount public Guid Id { get; set; } private ICollection<AccountTransaction> _transactions = new List<AccountTransaction>(); public IReadOnlyCollection<AccountTransaction> Transactions => _transactions .OrderByDescending(transaction => transaction.TransactionDateTime).ToList(); AccountTransaction public Guid Id { get; set; } public decimal Amount { get; set; ) I am trying to retrieve the fund

Execute RAW SQL on DbContext in EF Core 2.1

删除回忆录丶 提交于 2020-01-04 04:39:28
问题 I have researched this and always found examples like this: var blogs = context.Blogs .FromSql("SELECT * FROM dbo.Blogs") .ToList(); The problem is, I don't want to run my raw SQL on the Blogs table. Basically, I want to implement an interface like this: bool ExecuteNonSafeSql( string connectionString, string sql); Is there a way to do that with DbContext? 回答1: At the time of writing (EF Core 2.1), there is no way to execute arbitrary secuence returning SQL command. Only entity types and

Execute RAW SQL on DbContext in EF Core 2.1

天大地大妈咪最大 提交于 2020-01-04 04:39:03
问题 I have researched this and always found examples like this: var blogs = context.Blogs .FromSql("SELECT * FROM dbo.Blogs") .ToList(); The problem is, I don't want to run my raw SQL on the Blogs table. Basically, I want to implement an interface like this: bool ExecuteNonSafeSql( string connectionString, string sql); Is there a way to do that with DbContext? 回答1: At the time of writing (EF Core 2.1), there is no way to execute arbitrary secuence returning SQL command. Only entity types and

Get List of Entity Models in DbContext Entity Framework Core 2.1

孤街醉人 提交于 2020-01-02 06:26:08
问题 I'm trying to find a way to get a List of all of the entity models in my DbContext. For instance, if I have two models called Customer and Invoice defined in C# which through code-first I created EF entities and a database, how do I now query the DbContext to get a List that has Customer and Invoice in it -- i.e., all of the entities in that context? I want to be able to call a method that returns a List of all the entities -- not the data, just a list of the entities. It seems to me this

How to validate uploaded file in ASP.Net Core

£可爱£侵袭症+ 提交于 2020-01-01 15:06:11
问题 I'm using ASP.NET Core 2.2 and I'm using model binding for uploading file. This is my UserViewModel public class UserViewModel { [Required(ErrorMessage = "Please select a file.")] [DataType(DataType.Upload)] public IFormFile Photo { get; set; } } This is MyView @model UserViewModel <form method="post" asp-action="UploadPhoto" asp-controller="TestFileUpload" enctype="multipart/form-data"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input asp-for="Photo" /> <span asp

Getting “attempt was made to lazy-load navigation property on detached entity” despite eager fetch

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 06:29:19
问题 I'm using Entity Framework Core 2.1.2 with lazy loading enabled and am performing a query using AsNoTracking. I'm using Include to bring in my navigation property (a collection). If all my entities have at least one child in their collection then it all works fine. However, if any of my entities have no children then I get an error: System.InvalidOperationException: Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning: An attempt was made to

Populate Created and LastModified automagically in EF Core

十年热恋 提交于 2019-12-28 16:18:10
问题 Looking forward to build a framework, (No repository pattern to working with DbSets directly) to autopopulate Created and last modified automatically, rather than spitting out these codes through out code base. Could you point me out in right direction how to achieve it. In past I tried populating these in constructors, however that seems like a nasty code and every time we pull up somting from database EF change tracking will mark the entity as modified. .ctor() { Created = DateTime.Now;

EF Core - Connection string cannot be null

冷暖自知 提交于 2019-12-25 03:35:09
问题 I am using ASP.Net Core 2.1 with EF Core 2.1 While running any Migration from PMC, I am getting the below error Value cannot be null. Parameter name: connectionString This is how my Context class looks like public class MyAppContextFactory : IDesignTimeDbContextFactory<MyAppContext> { private readonly string _dbConnection; public MyAppContextFactory() { } public MyAppContextFactory(string dbConnection) : this() { _dbConnection = dbConnection; } public MyAppContext CreateDbContext(string[]

EF Core - Connection string cannot be null

ぐ巨炮叔叔 提交于 2019-12-25 03:35:03
问题 I am using ASP.Net Core 2.1 with EF Core 2.1 While running any Migration from PMC, I am getting the below error Value cannot be null. Parameter name: connectionString This is how my Context class looks like public class MyAppContextFactory : IDesignTimeDbContextFactory<MyAppContext> { private readonly string _dbConnection; public MyAppContextFactory() { } public MyAppContextFactory(string dbConnection) : this() { _dbConnection = dbConnection; } public MyAppContext CreateDbContext(string[]