entity-framework-core

change all string property max length

巧了我就是萌 提交于 2019-12-18 05:59:52
问题 In EF 6 I can do something like this: modelBuilder .Properties() .Where(p => p.PropertyType == typeof(string) && p.GetCustomAttributes(typeof(MaxLengthAttribute), false).Length == 0) .Configure(p => p.HasMaxLength(2000)); since EF7 ModelBuilder doesn't have the Properties() function, how do I do same thing in EF7? 回答1: I suppose this to be one of the "still lacking" functionalities in EF Core and expect it to be added in some later version. Until then, the closest I can suggest (for v1.1.0)

Entity Framework Core: many-to-many relationship with same entity

一笑奈何 提交于 2019-12-18 04:32:21
问题 I am trying to map many-to-many relationship with the same entity. The User entity has an IList<User> data field for Contacts , which stores users' contacts/friends information: public class User : DomainModel { public virtual IList<User> Contacts { get; protected set; } //irrelevant code omitted } When I try to use fluent API to map this many to many relationship, it gives me some trouble. Apparently, when I use HasMany() on the user.Contacts property, it has no WithMany() method to call

Raw SQL queries and Entity Framework Core

旧时模样 提交于 2019-12-18 03:23:13
问题 I migrate my application to ASP.NET MVC Core and Entity Framework Core and i found problem. I have raw SQL query to entity like this var rawSQL = dbContext.Database.SqlQuery<SomeModel>("Raw SQL Query").ToList(); But there is no SqlQuery<T> in context.Database . Do you have solution for this problem? 回答1: Make sure you add using Microsoft.Data.Entity; because there is an extension method you could use. var rawSQL = dbContext.SomeModels.FromSql("your SQL"); Even better, instead using raw SQL

Raw SQL queries and Entity Framework Core

时光怂恿深爱的人放手 提交于 2019-12-18 03:23:05
问题 I migrate my application to ASP.NET MVC Core and Entity Framework Core and i found problem. I have raw SQL query to entity like this var rawSQL = dbContext.Database.SqlQuery<SomeModel>("Raw SQL Query").ToList(); But there is no SqlQuery<T> in context.Database . Do you have solution for this problem? 回答1: Make sure you add using Microsoft.Data.Entity; because there is an extension method you could use. var rawSQL = dbContext.SomeModels.FromSql("your SQL"); Even better, instead using raw SQL

How to apply migrations from code (EF Core)

微笑、不失礼 提交于 2019-12-18 02:27:06
问题 How to apply migrations from code for EF6 work code Database.SetInitializer<CmContext>(null); var settings = new MigrationsConfiguration(); var migrator = new DbMigrator(settings); migrator.Update(); how to make similar in EF Core? 回答1: In beta 7 and on, use: using Microsoft.Data.Entity; ... context.Database.Migrate(); 回答2: For Entity Framework Core 1.0.0, ensure you have the Microsoft.EntityFrameworkCore.Relational NuGet package. Then import this namespace: using Microsoft

Include collection in Entity Framework Core

无人久伴 提交于 2019-12-17 22:45:59
问题 For example, I have those entities: public class Book { [Key] public string BookId { get; set; } public List<BookPage> Pages { get; set; } public string Text { get; set; } } public class BookPage { [Key] public string BookPageId { get; set; } public PageTitle PageTitle { get; set; } public int Number { get; set; } } public class PageTitle { [Key] public string PageTitleId { get; set; } public string Title { get; set; } } How should I load all PageTitles, if I know only the BookId? Here it is

ASP.NET Core with EF Core - DTO Collection mapping

佐手、 提交于 2019-12-17 22:43:02
问题 I am trying to use (POST/PUT) a DTO object with a collection of child objects from JavaScript to an ASP.NET Core (Web API) with an EF Core context as my data source. The main DTO class is something like this ( simplified of course ): public class CustomerDto { public int Id { get;set } ... public IList<PersonDto> SomePersons { get; set; } ... } What I don't really know is how to map this to the Customer entity class in a way that does not include a lot of code just for finding out which

Entity Framework Core RC2 table name pluralization

徘徊边缘 提交于 2019-12-17 22:22:14
问题 Is there a way to do what this code did in EF Core RC 2? protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } 回答1: There is no convention for this as of EF RC2 build. This is from EF Core team: In past pre-release of EF Core, the table name for an entity was the same as the entity class name. In RC2 we now use the name of the DbSet property. If no DbSet property is defined for the given entity type, then the

Expression tree to SQL with EF Core

爱⌒轻易说出口 提交于 2019-12-17 20:52:35
问题 We have a column where JSON data is stored as a string. This JSON data is read and converted through materialization to an IDictionary<string, object> . This all works fine until I want to filter on it. The filtering is only applied after getting the data from the database. We will have millions of records so this is not acceptable. My filter is being completely ignored as a WHERE clause by EF Core obviously since probably it has no idea how to parse the MethodCallExpressions. I'm looking for

Use a Inline Table-Valued Functions with Linq and Entity Framework Core

孤街浪徒 提交于 2019-12-17 20:36:53
问题 I created an Inline Table-Valued Functions (ITVF) in SQL Server that returns a table of values (query simplified for discussion purposes): CREATE FUNCTION dbo.VehicleRepairStatus() RETURNS TABLE AS RETURN SELECT VehicleID, CurrentStatus FROM VehicleRepairHistory ... Which I can reference in a query: SELECT v.ID, v.Name, r.CurrentStatus FROM Vehicle v LEFT OUTER JOIN dbo.VehicleRepairStatus() r on v.ID = r.VehicleID I'd like to be able to use it in Linq query: var vehicles = await