entity-framework-5

Filtering navigation property in eager loading

扶醉桌前 提交于 2019-12-22 10:09:55
问题 I have been working with soft delete and now i want to load the navigation properties of my entity that are not "deleted". I have found a way, my problem this way is not to clear for me, there is another way to do this. Context.CreateSet().Include("Salary").Select(u => new {User= u, Salary = u.Salarys.Where(s => !s.Deleted)}).AsQueryable().Select(a => a.User).AsQueryable(); 回答1: Eager loading doesn't support filtering. Your code can be simplified to: var users = Context.CreateSet<User>()

EF Code First migrations: Table Per Hierarchy Bug

你说的曾经没有我的故事 提交于 2019-12-22 09:56:02
问题 Often we might need to use Entity Framework Code First with an existing database. The existing database may have a structure the allows "Table Per Hierarchy" inheritance. Or we might start with an object model that looks like: public partial class Person { public int Id { get; set; } public string Discriminator { get; set; } public string Name { get; set; } public Nullable<int> StudentTypeId { get; set; } public virtual StudentType StudentType { get; set; } } public partial class StudentType

Entity Framework 5 Thread Agility

筅森魡賤 提交于 2019-12-22 09:55:28
问题 A NullReferenceException deep inside EntityFramework code is thrown (EF bug?), but my question is about Entity Framework (v5) and WebAPI asynchronous controller action. A repro would be hard to recreate here, but the code in essence does the following: public class AController : ApiController { private IUow _uow; //among other things, a DbContext // DI ctor public AController(IUow uow) { _uow = uow; } [HttpPost] public async Task<HttpResponseMessage> Post(Model model) { Entity e = _uow.Entity

How to get foreign key value for independent association without hitting database?

我们两清 提交于 2019-12-22 09:28:47
问题 I am using independent associations (with lazy loading) to access related entities in my code first model e.g. public class Aaa { public int AaaId {get;set;} public string SomeValue {get;set;} } public class Bbb { public int BbbId {get;set;} public string SomeValue {get;set;} public virtual Aaa MyIndependentAssociation {get;set;} } but I am wondering how to access the foreign key value of MyIndependentAssociation without actually loading the record. I am assuming the Aaa_AaaId value is

EF linq/lambda .contains(list[String])?

不打扰是莪最后的温柔 提交于 2019-12-22 08:59:25
问题 is there any way to evaluate if a string contains some of the elements of a list or all the elements of the list? using linq to entities? i have been trying to use predicateBuilder and others but im not %100 into thoses. EDIT something like: string[] words = searchString.Split(' '); var resultado = db.users .Where(u => u.fullName.contains(words) ) .Select(s => new { user_id = s.id_user, nombre = s.fullName}) .ToList(); 回答1: You need to reverse your use of Contains to check the words

Any workaround for lack of support for PadLeft in EF5.x?

徘徊边缘 提交于 2019-12-22 06:56:16
问题 I'm working on an app in MVC4 and Entity Framework 5 and recently came across this exception when executing my query. {"LINQ to Entities does not recognize the method 'System.String PadLeft(Int32, Char)' method, and this method cannot be translated into a store expression."} When I have run across similar errors in the past, I've just made a variable outside the query and then used the variable in the LINQ statement. Unfortunately, in this case I'm manipulating the row results so I'm not sure

How to define a custom naming convention if EF 5

吃可爱长大的小学妹 提交于 2019-12-22 06:47:36
问题 Suppose that I have a database with 1 table named Products . So, I go through the Db First approach and create an EF model in VS 2012 with pluralize and singularize option. So, the model creates a Product entity for me, and default naming convention maps this entity to the dbo.Products table. Now I want to change this behavior. In fact I want to create a custom convention to map ProductModel entity to the dbo.Products table. Is this possible?! If so, how? Update: My goal to do it... As you

Why become referential constraints inconsistent after updating foreign key?

一世执手 提交于 2019-12-22 06:44:36
问题 Sorry for the nebulous title, it's hard to describe this in a single line: I have 2 entities User and UserAddress , where User has 2 foreign keys DefaultInvoiceAddressId and DefaultDeliveryAddressId and UserAddress has a UserId foreign key. The user object has navigation properties for the default addresses ( DefaultInvoiceAddress and DefaultDeliveryAddress ) as well as one for all of his addresses: AllAddresses . The mapping etc. works, creating and updating users and addresses works too.

Regarding the new EF5 auto-compiled queries feature

◇◆丶佛笑我妖孽 提交于 2019-12-22 05:19:05
问题 I've made few tests regarding the new EF5 auto-compiled queries feature. The problem is that I don't see any difference in the performance. I've made one project with .NET 4.0 and EF4.0 without using compiled queries. I've made another project with .NET 4.0 and EF4.0 with compiled queries - there was 50% improvement in performance. And then I tried to create a project with EF 5.0, in which there was basically no difference in performance. All the projects were asp.net mvc (first two were

Entity Framework DbContext in Azure Web Role

半城伤御伤魂 提交于 2019-12-22 04:21:25
问题 I am migrating an existing Web Application (using Entity Framework 5) to an Azure Web Role. The database connection string is being moved from the web.config to the ServiceConfiguration.*.cscfg files. The problem is that in the auto-generated Model.Context.cs file, my entities class is defined like this: public partial class MyEntities : DbContext { public MyEntities() : base("name=MyEntities") { } // DbSets, etc } This will always look for MyEntities in the web.config . How can I override