entity-framework

Many-to-many query in Entity Framework 7

假装没事ソ 提交于 2021-02-04 18:07:35
问题 I am following this example which I got from http://ef.readthedocs.org/en/latest/modeling/relationships.html class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag

Many-to-many query in Entity Framework 7

99封情书 提交于 2021-02-04 18:07:17
问题 I am following this example which I got from http://ef.readthedocs.org/en/latest/modeling/relationships.html class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag

Entity Framework Core - Migration - No Parameterless Constructor Defined for this Object

不问归期 提交于 2021-02-04 17:19:45
问题 I am working with the latest .Net Core and EF Core in Visual Studio 2017. I have created a model and it was working great. I have since made some modifications and am getting the following error when I try to add a new migration: Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:09.08 System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&

Select all columns but group by only one in linq

会有一股神秘感。 提交于 2021-02-04 13:49:25
问题 I have been looking for a way to get multiple columns but group by only one in SQL and I found some info. However I can not came up with a way to do it in linq. I have the following toy example table: | Id | Message | GroupId | Date | |-------------------------------| | 1 | Hello | 1 | 1:00 | | 2 | Hello | 1 | 1:01 | | 3 | Hey | 2 | 2:00 | | 4 | Dude | 3 | 3:00 | | 5 | Dude | 3 | 3:01 | And I would like to recover all columns for the rows that have a distinct GroupId as follows (with a 'Date'

How can i write SQL update query with where clause in Entity Framework in C#

我的未来我决定 提交于 2021-02-04 12:41:08
问题 I know that for update a model in EF i must get model then change it then dbContext.SaveChanges() but sometimes in my Asp.net mvc project , i want update a field in my table and i don't know it 's id and must be get it with where clause. but i don't want connect twice to database , because in ADO.net i can write: UPDATE MyTable SET Field3 = "NewValue" WHERE Active = 1 and now i want write a linq to sql for EF that work like that. Is exist any way for that? thanks 回答1: You can't. EF is ORM,

Replacing a entity collection in Entity Framework Core causes DbContext to fetch the new values when not saved to db. How to reload the collection?

99封情书 提交于 2021-02-04 08:36:08
问题 What I can't understand fully is exemplified in a unit test below. Basically I want to know why DbContext overrides collection values currently stored in the database even if Reload() and a new load for the collection has been called. If I try to call dbContext.Entry(test.TestChildren).Reload(); I get the exception: System.InvalidOperationException: 'The entity type 'List' was not found. Ensure that the entity type has been added to the model.' I have also loaded TestChildren explicitly via

FirebirdSql.Data.FirebirdClient.FbException: too many open handles to database

社会主义新天地 提交于 2021-02-04 07:28:05
问题 Today I did a larger data import into a firebird 2.5.6 database and I got this exception: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> FirebirdSql.Data.FirebirdClient.FbException: too many open handles to database ---> FirebirdSql.Data.Common.IscException: too many open handles to database bei FirebirdSql.Data.Client.Managed.Version10.GdsDatabase.ProcessResponse(IResponse response)

How to resolve error with enitiy freamwork include

时光怂恿深爱的人放手 提交于 2021-01-29 21:52:35
问题 I write app using .Net 5 and Entity Freamwork Core. I have method that is looking for some data. It looks like this: public async Task<Result<List<Company>>> Search(string keyword, DateTime From, DateTime To, JobTitle jobTitle) { return Result.Ok(await _dataContext.Companies .Include(x => x.Employes .WhereIf(From != default && To != default, x => x.DateOfBirth >= From && x.DateOfBirth <= To && x.JobTitle == jobTitle)) .WhereIf(!string.IsNullOrEmpty(keyword), x => x.Name.Contains(keyword))

Addresses database schema for multiple entities

ⅰ亾dé卋堺 提交于 2021-01-29 19:53:22
问题 I'm playing around with entity framework and code first approach. The scenario is this: An user can have multiple companies (each company has an address) An user can have multiple houses per company (each house has an address) I'm thinking of two ways I can manage addresses: Have an Address table with a column for CompanyId and HouseId (for companies addresses only CompanyId will be inserted and for houses both Ids will be inserted. Have a CompanyAddress and HouseAddress tables with the only

Performance issue when performing operations on entity objects

馋奶兔 提交于 2021-01-29 18:51:15
问题 Im facing performance issue in below code in multiple foreach loops. First im getting a list of ReturnDetails and then based on detail id get the HandlingInfo object. Then based on value of action, update the ReturnsDetail Object again. It take more than a minute for loading 3000 records of ReturnsDetail. While debugging locally, it runs for infinite amount of time. Please let me know in anyway i can refactor this code . Thanks for your help. lstReturnsDetail = dcReturnsService