entity-framework

DbContext Multiple Inheritance

倾然丶 夕夏残阳落幕 提交于 2020-01-05 11:01:22
问题 The design of EF forces developers to inherit the DbContext class. Some reusable libraries (such as ASP.NET Identity) typically provides its functionality using the same inheritance route, i.e. by providing the IdentityDbContext base-class. But obviously this won't work if you have 2 such libraries, e.g. requiring you to inherit from IdentityDbContext and CmsDbContext at the same time, which is obviously impossible to do on .NET. The result that I want is to have an application-dbcontext that

How to grab the index from a list using LINQ

江枫思渺然 提交于 2020-01-05 10:26:15
问题 I have a list I am populating with total sales from a team. lstTeamSales.OrderBy(x => x.TotalSales); This list has an int userID and a decimal totalSales . I order it by totalSales . How can I at that point figure out the rank for the person logged in? I know I can compare the person who is logged in by his userID to that of the userID in the list. If he is #3 in sales I need to return an int of his rank which would be Rank 3. 回答1: The question can be rephrased to "How do I get index of

Code First - Retrieve and Update Record in a Transaction without Deadlocks

瘦欲@ 提交于 2020-01-05 10:24:41
问题 I have a EF code first context which represents a queue of jobs which a processing application can retrieve and run. These processing applications can be running on different machines but pointing at the same database. The context provides a method that returns a QueueItem if there is any work to do, or null, called CollectQueueItem . To ensure no two applications can pick up the same job, the collection takes place in a transaction with an ISOLATION LEVEL of REPEATABLE READ . This means that

LINQ group-by and then display datetime for (dd mmm)

﹥>﹥吖頭↗ 提交于 2020-01-05 10:16:05
问题 I want Linq to group by date but display in text here is my code var groups = _uow.Orders.GetAll() .Where(x => x.Created > baselineDate) .GroupBy(x => x.Created.ToString("yyyy-MM-dd")); var orders = new { Day = groups.Select(g => g.Key).ToArray(), Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(), }; the result is (not good to put in label of graph) {"Day": [2, 3, 4, 5], "Total": [9999.00, 9999.00, 9999.00, 9999.00] } But i want this (Monthly) "Day": ['Jan', Feb', 'Mar', 'Apr'],

Does Include load all related entities or the specified ones?

半腔热情 提交于 2020-01-05 10:12:23
问题 When going through a learning article about related entity loading in Entity Framework MSDN, I came across the following: It is also possible to eagerly load multiple levels of related entities . The queries below show examples of how to do this for both collection and reference navigation properties. ...[Examples demonstrating the above]... Note that it is not currently possible to filter which related entities are loaded . Include will always being in all related entities. This seems

Does Include load all related entities or the specified ones?

会有一股神秘感。 提交于 2020-01-05 10:11:48
问题 When going through a learning article about related entity loading in Entity Framework MSDN, I came across the following: It is also possible to eagerly load multiple levels of related entities . The queries below show examples of how to do this for both collection and reference navigation properties. ...[Examples demonstrating the above]... Note that it is not currently possible to filter which related entities are loaded . Include will always being in all related entities. This seems

Required Model Validation Generating the Models using Entity Framework Database First Approach

孤街醉人 提交于 2020-01-05 09:35:20
问题 In continuation with the question asked here, even with EF 6, I am having issues generating the [Required] Data Annotation for certain fields for a model autogenerated from Database Table. Am I missing something or is it still not supported? The DB fields that I am looking at are "NOT NULL". Any help will be appreciated! I have too many tables and fields to look at and automating this process via Entity framework will be nice to have! 来源: https://stackoverflow.com/questions/29827993/required

System.ObjectDisposedException when trying to receive the data from a List

笑着哭i 提交于 2020-01-05 09:23:30
问题 Hello I am writing an ASP.Net MVC application and I have a special class dedicated for Database connection. In my HomeController I call the static methods of this special DB class, which return the needed data into objects. I use the Entity Framework in order to achieve this. However I get a strange exception when I try to use the List from my Controller. I believe that the problem is that I have a virtual inner collection that is disposed after the entity framework methods are done. I can

What is the return value of Database.ExecuteSqlCommand?

折月煮酒 提交于 2020-01-05 09:23:19
问题 Modifying an answer from this question slightly, suppose I run this code: public int SaveOrUpdate(MyEntity entity) { var sql = @"MERGE INTO MyEntity USING ( SELECT @id as Id @myField AS MyField ) AS entity ON MyEntity.Id = entity.Id WHEN MATCHED THEN UPDATE SET Id = @id MyField = @myField WHEN NOT MATCHED THEN INSERT (Id, MyField) VALUES (@Id, @myField);" object[] parameters = { new SqlParameter("@id", entity.Id), new SqlParameter("@myField", entity.myField) }; return context.Database

What is the return value of Database.ExecuteSqlCommand?

半腔热情 提交于 2020-01-05 09:23:16
问题 Modifying an answer from this question slightly, suppose I run this code: public int SaveOrUpdate(MyEntity entity) { var sql = @"MERGE INTO MyEntity USING ( SELECT @id as Id @myField AS MyField ) AS entity ON MyEntity.Id = entity.Id WHEN MATCHED THEN UPDATE SET Id = @id MyField = @myField WHEN NOT MATCHED THEN INSERT (Id, MyField) VALUES (@Id, @myField);" object[] parameters = { new SqlParameter("@id", entity.Id), new SqlParameter("@myField", entity.myField) }; return context.Database