entity-framework-6

How can I find the Id property or properties related to a navigational property?

*爱你&永不变心* 提交于 2019-12-01 07:24:26
问题 For a project I'm working with Entity Framework and I'd like to be able to enumerate all navigational properties for a given object instance (assuming it's an object generated by EF). From there I'd like to get the related Id property for every navigational property. For example, if I get an instance of the class Person , I want to be able to find it's navigational properties called Address and Boss . For those two navigational properties I want to then "lookup" the related Id properties

ObjectContext.GetObjectType(e.GetType()) not returning the entity type of the POCO entity

限于喜欢 提交于 2019-12-01 07:21:27
问题 The ObjectContext.GetObjectType Method should return "the entity type of the POCO entity associated with a proxy object of a specified type" So how come in my code it just returns the proxy? I am using entity framework 6 release candidate //Soft delete var e = Context.Set<T>().Find(id); e.IsDeleted = true; InsertOrUpdate(e); Type t = System.Data.Objects.ObjectContext.GetObjectType(e.GetType()); string name = t.Name; //Property_6C887DE7274181E6E99D6FCF2C21BDD59E226F99B0064F59954E70062C135331 /

Getting COUNT and SKIP TAKE in one operation with Linq to Entities

痞子三分冷 提交于 2019-12-01 07:19:58
I have a data call in a Linq to Entities powered data access layer that is designed to make paged calls. In doing so, I need to select a subset of the data, say 50 rows, but also get the count of all matches to know how many total matches exist to be paged through. Currently, I'm doing the following: var queryResult = DatabaseContext.Table .Where(x => !x.IsDeleted) .Where(p => ( p.PropertyOne.ToLower().Contains(query) || p.PropertyTwo.ToLower().Contains(query) )); int count = queryResult.Count(); var returnData = queryResult .OrderBy(i => i.ID) .Skip(start).Take((length)) .Select(y => new

Entity Framework complex type's columns naming convention

让人想犯罪 __ 提交于 2019-12-01 06:57:22
Using complex types the default column naming convention uses underscore. That means having type defined that way: [ColmplexType] public class Contact { string Email {get;set;} string Post {get;set;} } public EntityN { //... public Contact Contact {get;set;} } we will get columns named such way Contact_Email nvarchar(max) Contact_Post nvarchar(max) We of course could configure each column name separately using ColumnAttribute or Context.Properties mapping, but do we have a possibility to create naming convention and therefore configure all names in once for currnet type? For some of complex

Nullable fields are created when customizing IdentityUser class in asp.net Identity

老子叫甜甜 提交于 2019-12-01 06:47:15
问题 I am trying to customize IdentityUser class in asp.net identity. public class ApplicationUser : IdentityUser { public ApplicationUser() { IsBlocked = false; } public bool IsBlocked { get; set; } } The problem is: when using code first migrations, the additional field is created nullable. The same if I drop database and recreate it. CREATE TABLE [dbo].[AspNetUsers] ( [Id] NVARCHAR (128) NOT NULL, [UserName] NVARCHAR (MAX) NULL, [PasswordHash] NVARCHAR (MAX) NULL, [SecurityStamp] NVARCHAR (MAX)

Emit DbContext.OnModelCreating on every context creation

梦想的初衷 提交于 2019-12-01 06:36:23
问题 I use entity framework code first to work with my database. I have several tables with different names but same structure, and this tables dynamically appears in database. How could I map EntityFramework to one of that tables at run-time and use data from just like I work this over entities of DbContext? What I've done to make it work: For example, my class what describes structure of dynamically created table is SetElement . Here is my context: public class DataContext : DbContext { public

EF 6 Lazy Loading Disabled but Child Record Loads Anyway

回眸只為那壹抹淺笑 提交于 2019-12-01 06:27:43
I'm using EF6 code first. There are two tables, Lesson and LessonSections . The LessonSections table has a foreign key to Lesson.Id Here is the Lesson class with none important fields removed: public partial class Lesson { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Lesson() { LessonSections = new HashSet<LessonSection>(); } [StringLength(50)] public string Id { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual

Entity Framework will only set related entity property to “null” if I first get the property

强颜欢笑 提交于 2019-12-01 06:21:12
Edit This seems to occur for any Entity property that references another entity in one direction. In other words, for the below example, the fact that Bar overrides Equality appears to be irrelevant. Suppose I have the following classes: public class Foo { public int? Id { get; set; } public virtual Bar { get; set; } } public class Bar : IEquatable<Bar> { public int Id { get; set; } public override bool Equals(object obj) { var other = obj as Bar; return Equals(other); } public bool Equals(Bar other) { if (object.Equals(other, null)) return false; return this.Id == other.Id; } public static

Projecting self referencing multi level Entities In Entity Framework 6

天大地大妈咪最大 提交于 2019-12-01 06:15:30
Projecting self referencing multi level entities in Entity Framework 6. Let's say that I have a Category entity as follows: public class Category { public int CategoryId { get; set; } public int? ParentCategoryId { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual Category ParentCategory { get; set; } public virtual ICollection<Category> SubCategories { get; set; } public virtual ICollection<Product> Products { get; set; } public Category() { SubCategories = new HashSet<Category>(); Products = new HashSet<Product>(); } } And I would like to map

EF6 alpha Async Await on an Entity Stored Procedure / Function Import?

一曲冷凌霜 提交于 2019-12-01 05:29:32
I'd like to apply the new async await functionality to Stored Procedures / Function Imports imported in my Entity model, but have as yet been unable to with the EF6 alpha. Is it yet possible in EF6 alpha2 (or the nightly build as of 20211) to call any of the new Async methods on an Entity Function Import (which calls a SQL Stored Procedure) that returns a collection of Complex Type? e.g. private async Task<IList<Company>> getInfo (string id) { using (CustomEntity context = new CustomEntity()) { var query = await context.customStoredProcedure(id).ToListAsync(); // ".ToListAsync()" method not