entity-framework-core

How do I simplify the access of a has-many relationship with the entity framework?

筅森魡賤 提交于 2019-12-10 17:33:15
问题 Here is what I want to do: var user = db.User.First(conditions); user.Book.First(); Here is currently how I have to do that. var user = db.User.Include("Book").First(conditionsForUser); user.Book.First(); The reason why I want to simplify this, is because I don't want to have to specify what is included every time I want to access a relationship. Seems very cumbersome. e.g.: I would like to just be able to do the following, given I have previously retrieved a user: user.Book.First() user.Blog

Entity Framework 7 and SQLite Tables not creating

孤街醉人 提交于 2019-12-10 17:24:23
问题 I've been trying for awhile to figure out how to use a single DBContext to create multiple tables in a Code First fashion without any luck. I'm sure it's just my unfamiliarity with the framework but I'm not sure what I'm missing. Here's a simple example with entities and the DBContext. [Table("MyEntity")] public class MyEntity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string MyColumn { get; set; } public int MyNumber { get; set; } }

SelectMany applied to 3 lists

旧巷老猫 提交于 2019-12-10 17:18:24
问题 I need to create a list of objects, OrganizationEnrolment, created using 3 Entity Framework entities (User, EnrolmentType and OrganizationEnrolment): List<OrganizationEnrolment> organizationEnrolments = context.Organizations .SelectMany(x => context.Users) .SelectMany(x => context.EnrolmentTypes) .Select(y => new OrganizationEnrolment { EnrolmentType = enrolmentType Organization = organization, User = user }) My problem is after having the SelectMany how to get the enrolmentType, organization

How to define multiple relationships between two entities?

做~自己de王妃 提交于 2019-12-10 17:17:40
问题 I have the scenario where a user can upload multiple photos (One-to-Many). The user also can have a default photo (One-to-One). However, I entity framework core 2.0 tells that he cannot recognize the relationship when I use the following code: public class User { public Guid Id { get; set; } public ICollection<Photo> Photos{ get; set; } public Photo DefaultPhoto { get; set; } [ForeignKey("DefaultPhoto")] public Guid DefaultPhotoId { get; set; } } public class Photo { public Guid Id { get; set

EF.Core 2.0.1 Unsafe in Xamarin.Android

大城市里の小女人 提交于 2019-12-10 17:03:40
问题 I'm not sure what to do about this. Any suggestions? Deployment Exception D/Mono (12535): Assembly Loader probing location: 'System.Runtime.CompilerServices.Unsafe'. F/monodroid-assembly(12535): Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration. F/monodroid-assembly(12535): This might be due to an invalid debug installation. F/monodroid-assembly(12535): A common cause is to 'adb install' the app directly instead of doing from the IDE. Repro Visual

Include() ThenInclude() throws “Sequence contains more than one matching element” exception in Table Per Hierarchy strategy

折月煮酒 提交于 2019-12-10 17:00:34
问题 I am working with Entity Framework 7 and code-first, and I have a model which involves parent-child relations on 3 levels : Corporations have companies Companies belong to a corporation and have factories Factories belong to a company Since these 3 entities share a lot in common, they all inherit from an abstract BaseOrganization entity. When I try to list all the factories, including their mother companies, and then including their mother corporations, I have these two different scenarios :

How to UPDATE modified and deleted entities on SaveContext?

余生颓废 提交于 2019-12-10 16:59:29
问题 Goal is to track who had changed and deleted an entity. So I have an entity that implements an interface: interface IAuditable { string ModifiedBy {get;set;} } class User: IAuditable { public int UserId {get;set;} public string UserName {get;set;} public string ModifiedBy {get;set;} [Timestamp] public byte[] RowVersion { get; set; } } Now the code of entity remove operation could look like this : User user = context.Users.First(); user.ModifiedBy = CurrentUser.Name; context.Users.Remove

How to run a migration in UWP using ef core 1

旧时模样 提交于 2019-12-10 16:29:51
问题 I don't know how I have run the migration in EntityFramework.Core(v7.0.0-rc1-final) using EntityFramework.Commands(also v7.0.0-rc1-final). When I add the migration (Add-Migration) so migration creates. Then, when I enter the Update-Database, so the PM console returns: Update-Database should not be used with Universal Windows apps. Instead, call DbContext. Database. Migrate () at runtime. But the Context.Database does not contain method Migrate(). So this command cannot be specified. It seems

Where is HasDefaultSchema in Entity Framework Core?

£可爱£侵袭症+ 提交于 2019-12-10 16:25:51
问题 I'm porting my EF to EF Core. There doesn't appear to be a HasDefaultSchema method in the new API. What is the equivalent of this code EF Core? protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("Notifications"); } 回答1: It's exactly the same as the sample code. The only difference with EF6 is that as most of the EF Core fluent (and not only) method it's implemented as extension method (rather than instance method) of the ModelBuilder class

EF core migration failed: System.Runtime.Remoting.RemotingException: Cannot load type 'Microsoft.EntityFrameworkCore.Design.IOperationResultHandler

余生长醉 提交于 2019-12-10 16:22:54
问题 I made Entity Framework core migration first time to my average size project (about 100 tables) and now I am trying to do changes to the azure sql db with 'Update-Database' call in package manager console. I will get this error always: Exception message: Stack trace: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.Remoting.RemotingException: Cannot load type 'Microsoft.EntityFrameworkCore.Design.IOperationResultHandler