entity-framework-core

Entity Framework Core nullable foreign key

孤者浪人 提交于 2019-12-10 13:05:19
问题 I am having trouble getting a list of entities where a foreign key value could be null. The Models: public class Company { [Key] [Required] public int Id { get; set; } [Display(Name = "Company Name")] public string CompanyName { get; set; } [Display(Name = "Main Phone")] public string LandPhone { get; set; } [Display(Name = "Fax")] public string FaxPhone { get; set; } } public class Contact { [Key] [Required] public int Id { get; set; } [Display(Name ="First Name")] public string FirstName {

Asp.net Core Get User by User Id

会有一股神秘感。 提交于 2019-12-10 12:53:43
问题 Using Asp.net Core Identity and entity framework core, how can I get an asp.net user object by it's UserId property? I know it's possible to get the current logged in user from the HttpContext.User object using: UserManager.GetUserAsync(HttpContext.User) However when an admin is logged in, I want them to be able to access/modify any user's data in order to update user Emails and PhoneNumbers, which by default are part of the asp.net user object. Ideally I'd like to be able to do something

EF 7 beta 6 : Entities with one to many relationships in EF 7

≯℡__Kan透↙ 提交于 2019-12-10 12:53:17
问题 I have a very simple model: class Bag { public int Id { get; set; } public ICollection<RandomThing> RandomThings { get; set; } } class RandomThing { public int Id { get; set; } public String Description{ get; set; } } My context's OnModelCreating (in EF 6 ) is something like: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Bag>() .ToTable("Bag") .HasMany(x => x.RandomThings).WithRequired(x => x.Bag)

Entity Framework 7 Migration Scaffolding in Class Library with Configuration

泪湿孤枕 提交于 2019-12-10 12:36:30
问题 Trying to add migrations to an EF7 Model that lives in an ASP.NET 5 class library. When running dnx . ef migration add mymigration fails with different results depending on which project I run it on. If I run it in the folder of the main project, it cannot locate a DbContext , this makes sense because the DbContext is in the shared project and ef commands likely don't care about dependencies. If I run it in the folder of the shared project, it does not have access to the connection string

Create Unique constraint for 'true' only in EF Core

泄露秘密 提交于 2019-12-10 10:17:50
问题 I have a class for tracking attachments to a Record. Each Record can have multiple RecordAttachments, but there is a requirement that there can only be one RecordAttachment per-Record that is marked as IsPrimary . public class RecordAttachment { public int Id { get; set; } public int RecordId { get; set; } public string Details { get; set; } public bool IsPrimary { get; set; } public Record Record { get; set; } } I can't just use .HasIndex(e => new { e.RecordId, e.IsPrimary }).IsUnique(true)

EF Core insert explicit value for identity column

試著忘記壹切 提交于 2019-12-10 09:58:41
问题 I have problem with inserting data to database table which has foreign keys. As error I'm getting Cannot insert explicit value for identity column in table 'MachineTypes' when IDENTITY_INSERT is set to OFF.Cannot insert explicit value for identity column in table 'SpareTypes' when IDENTITY_INSERT is set to OFF. BaseEntity.cs public abstract class BaseEntity { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public Int64 Id { get; set; } public DateTime CreateDate { get; set; } }

EF Core self referencing not working if there are 2 foreign keys to self (Code first)

故事扮演 提交于 2019-12-10 09:40:32
问题 If I defined (in code first) one navigation property to self it works (foreign key is created), but if I define 2 it doesn't work. How can I create 2 foreign keys to self? Based on documentation by conventions they should be created. For example this works (foreign key is created): public class DbPart { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [ForeignKey("Source")] public int? SourceId { get; set; } public DbPart Source { get; set; }

Entity Framework Core Auto Generated guid

余生颓废 提交于 2019-12-10 09:33:44
问题 Can some One guide me I want primeryKey of a table as guid having db generated value on insert. [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } but it's giving error The seed entity for entity type 'User' cannot be added because there was no value provided for the required property 'Id'. Here is my actual model classes and DbContxt class: public class BaseModel { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; }

StartsWith() doesn't translate to Like('abc%') in LINQ

泪湿孤枕 提交于 2019-12-10 07:41:19
问题 I have the following asp.net core LINQ code: List<UserSearchResult> results = await db.ApplicationUsers.Where(u => u.Name.StartsWith(name) && !u.Deleted && u.AppearInSearch) .OrderByDescending(u => u.Verified) .ThenBy(u => u.DateAdded) // Added to prevent duplication of results in different pages .Skip(page * recordsInPage) .Take(recordsInPage) .Select(u => new UserSearchResult() { Name = u.Name, Verified = u.Verified, PhotoURL = u.PhotoURL, UserID = u.Id, Subdomain = u.Subdomain })

The specified deps.json '$$$' does not exist

吃可爱长大的小学妹 提交于 2019-12-10 04:10:37
问题 I am rather new to the .NET Core, and I got a .NET Core WebAPI project MyWebApp, also, i have .Net Core Class Library project MyLib using EntityFrameworkCore When i try to use Add-Migration, i get the error The specified deps.json [...\MyWebApp\bin\Debug\netcoreapp1.1\MyWebApp.deps.json] does not exist Inspecting the folder, I noticed I have this a file in [...\MyWebApp\bin\Debug\netcoreapp1.1\win10-x64\MyWebApp.deps.json] but i really can't figure what i am supposed to do to resolve this.