entity-framework-core

Select specific properties from include ones in entity framework core

柔情痞子 提交于 2019-12-01 00:36:17
问题 I use Entity Framework Core 2.0 and have this 2 classes: News public class News { public int Id { get; set; } public string Title{ get; set; } public string Text { get; set; } public DateTime ReleaseDate{ get; set; } public int AuthorID{ get; set; } public Author Author{ get; set; } } Author public class Author { public Author () { News = new List<News>(); } public int Id { get; set; } public string Username{ get; set; } public string Firstname{ get; set; } public string Lastname{ get; set; }

EF 7 Migrations with multiple DBContexts

℡╲_俬逩灬. 提交于 2019-12-01 00:04:32
I have a problem scaffolding and doing migrations from a class library with multiple DBContexts. I found a command line argument that looks like this for migrations: dnx ef migration add -c Contexts.IndustryContext initial But this doesn't even get by the command line parser. I want all my DBContexts and database stuff out of the main MVC 6 web project and in their own DLLs. Is this possible? What command line magic is required? I was looking for an answer to this question and wanted to provide my solution for ef core 2.0. Microsoft.EntityFrameworkCore.Tools.DotNet needs to be added to each of

How to add where clause to ThenInclude

£可爱£侵袭症+ 提交于 2019-11-30 23:57:41
问题 I have 3 entities: Questionnaire.cs : public class Questionnaire { public int Id { get; set; } public string Name { get; set; } public ICollection<Question> Questions { get; set; } } Question.cs : public class Question { public int Id { get; set; } public string Text { get; set; } public ICollection<Answer> Answers { get; set; } } and Answer.cs : public class Answer { public int Id { get; set; } public string UserId { get; set; } public string TextAnswer { get; set; } } So I saved the

How to configure an Identity column using Entity Framework Core?

人盡茶涼 提交于 2019-11-30 22:31:08
问题 How do I create an Auto increment identity column in Entity Framework Core? Obviously I can do it using fluent API for EF6 for example. 回答1: Since there is very little EF7 documentation, much of what we know we have to glean from the source or unit tests. According to the following two unit tests in the EF7 source... Here and Here You would configure a property for Identity like this: b.Property(e => e.Id).ForSqlServer().UseIdentity(); And you would configure a property for Sequences like

Entity Framework not including columns with default value in insert into query

雨燕双飞 提交于 2019-11-30 21:54:27
I have a model that has some columns defined with default values like table.Column<bool>(nullable: false, defaultValueSql: "1") When I save a new entity in the database using context.SaveChanges() , I noticed that the columns with default values are not included in the insert into query that Entity Framework generates, so the values generated in the database are the default ones instead of the ones I'm passing in the model. Do I have to set up some property in the context to be able to set these properties through code? I'm using EF Core, but I don't know if this is a general behavior of all

What is the best way to perform partial updates in EF core and never update certain properties?

好久不见. 提交于 2019-11-30 21:08:21
I know you can do something like var myObj = _db.MyTable.FirstOrDefault(x=>x.Id==id) and then update myObj property by property that you want to update but is there a better way to update say 6 out of 10 properties of myObj and leave the other 4 alone or have them marked as a way that they are only set once and never updateable from ef core? public class MyObject { public string Id { get; set; } public string Prop1 { get; set; } public string Prop2 { get; set; } public string Prop3 { get; set; } public string Prop4 { get; set; } public string Prop5 { get; set; } public string Prop6 { get; set;

Retrieve child entities from CrudAppService in ABP

微笑、不失礼 提交于 2019-11-30 20:30:52
问题 The GetAll and Get methods of the ready-made CrudAppService don't include child entities. Is it possible to modify its behaviour? Update GetAllIncluding has some problem if the included entity has a navigation property to the parent; it falls into a sort of circular dependency. Is there any Attribute or trick to exclude the navigation property from the serialization? The [NonSerialized] attribute does not seem to be applicable to a navigation property. PostAppService : public class

How to create a Clustered Index with Entity Framework Core

三世轮回 提交于 2019-11-30 19:56:48
From EF6.1, we have a way of specifying a clustered index on a property public class Person { [Index(IsClustered = true, IsUnique = true)] public long UserName { get; set; } } But this Index attribute does not seem to be in EF Core right now? In EF Core how do you achieve this? From the current EF Core documentation - Indexes section: Data Annotations Indexes can not be created using data annotations. But for sure you can specify that via Fluent API (note the extension methods having ForSqlServer prefix which seem to denote SqlServer specific features): modelBuilder.Entity<Person>() .HasIndex

Preventing tracking issues when using EF Core SqlLite in Unit Tests

ε祈祈猫儿з 提交于 2019-11-30 19:07:17
I'm writing a unit test to test a controller action that updates an EF core entity. I am using SQLLite, rather than mocking. I set up my database like this: internal static ApplicationDbContext GetInMemoryApplicationIdentityContext() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder<ApplicationDbContext>() .UseSqlite(connection) .Options; var context = new ApplicationDbContext(options); context.Database.EnsureCreated(); return context; and then add an entity to the database like this: private DiaryEntriesController

No mapping to a relational type can be found for the CLR type 'Int32[]'

戏子无情 提交于 2019-11-30 19:02:59
When I execute a SQL Server stored procedure from Entity Framework Core (v2.0) in my ASP.NET Core project, I get this exception: InvalidOperationException: no mapping to a relational type can be found for the CLR type 'Int32[]' The SQL Server stored procedure code looks like this: CREATE PROCEDURE [dbo].[sp-UpdateProductOrderAndStock] @customerOrderID INT, @qty INT AS DECLARE @customerProductID INT SET @customerProductID = (SELECT CustomerProductID FROM dbo.CustomerOrder WHERE ID = @customerOrderID) SET NOCOUNT ON; UPDATE dbo.CustomerOrder SET CompletedQty = CompletedQty + @qty WHERE ID =