entity-framework

LINQ convert DateTime to string

主宰稳场 提交于 2020-01-09 11:14:13
问题 List<Post> list = ( from c in db.TitleComments join t in db.Titles on c.TitleId equals t.Id join u in db.Users on c.UserId equals u.Id where t.Id == _titleId && c.Date > time orderby c.Date descending select new Post { Username = u.Username, PostingDate = c.Date.ToString(), Data = c.Comment } ).ToList(); The code above causes exception on the convertion of date to string, PostingDate = c.Date.ToString(). Any ideas how to get around this? Exception error: {"LINQ to Entities does not recognize

linq to entity framework: use dictionary in query

谁说胖子不能爱 提交于 2020-01-09 11:13:42
问题 I have: Dictionary<int, List<int>> dict = new ... var a = SomeEntity.Where(f => dict[f.key].Contains(f.someValue)) this produces error LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[System.Int32] get_Item(Int32)' method while with lists it works: List<int> l = new ... var a = SomeEntity.Where(f => l.Contains(f.someValue)) So is this limitation of linq to EF or am I missing something? 回答1: It's the nature of Entity Framework - when you put expressions into

LINQ convert DateTime to string

折月煮酒 提交于 2020-01-09 11:11:31
问题 List<Post> list = ( from c in db.TitleComments join t in db.Titles on c.TitleId equals t.Id join u in db.Users on c.UserId equals u.Id where t.Id == _titleId && c.Date > time orderby c.Date descending select new Post { Username = u.Username, PostingDate = c.Date.ToString(), Data = c.Comment } ).ToList(); The code above causes exception on the convertion of date to string, PostingDate = c.Date.ToString(). Any ideas how to get around this? Exception error: {"LINQ to Entities does not recognize

Function Imports in Entity Model with a non-Entity Return Type

橙三吉。 提交于 2020-01-09 11:07:51
问题 I have a stored procedure in my Entity Data Model and added it to the function imports. Problem is... Visual Studio generates the function code in the model's code-behind if and only if I specify the return to be an entity type. Scalar and null return types do not work. Visual Studio does not generate the function code when I choose them. Is there something I am missing, or is this a bug? Any work-arounds? Using Visual Studio 2008 v9.0.30729.1 SP (Service Pack 1) 回答1: It's not so much a bug

Static field of DbContext in Global.asax versus instance field of DbContext in controller class?

一个人想着一个人 提交于 2020-01-09 10:30:09
问题 Frankly I am a newbie in both C# and Asp.net MVC. I also do not know how the asp.net web application actually works on IIS and ASP.NET framework behind the scene. I am confused with the decision where I have to declare a field of DbContext (or any class derived from DbContext) in my asp.net mvc application. I have two choices: Declare the field as a static field inside global.asax such that all controllers can make use of it. Declare the field as an instance field inside each controller class

LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])

早过忘川 提交于 2020-01-09 10:19:30
问题 I can't figure out why I'm getting this error. I have used this function successfully with previous versions of Entity Framework but I've set up a new project using EF6 and it's not cooperating. using System.Data; using System.Data.Objects.SqlClient; e.Result = from n in MyDB.tblBulletins where n.AnncStart <= DateTime.Now && n.AnncEnd > DateTime.Now && n.Approved == true orderby n.AnncStart descending, n.AnncDate descending select new { n.RecID, AnncTitle = n.AnncTitle + " <a href=

LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])

百般思念 提交于 2020-01-09 10:19:08
问题 I can't figure out why I'm getting this error. I have used this function successfully with previous versions of Entity Framework but I've set up a new project using EF6 and it's not cooperating. using System.Data; using System.Data.Objects.SqlClient; e.Result = from n in MyDB.tblBulletins where n.AnncStart <= DateTime.Now && n.AnncEnd > DateTime.Now && n.Approved == true orderby n.AnncStart descending, n.AnncDate descending select new { n.RecID, AnncTitle = n.AnncTitle + " <a href=

Complete Insert/Update/Delete of Child Entities in Entity Framework

别来无恙 提交于 2020-01-09 10:06:15
问题 I know it has been asked before, but after long hours of searching and coding I can't get to a working and clean approach. Here is what I have: public class QuestionModel { public int QuestionID { get; set; } public string QuestionText { get; set; } public IList<QuestionChoiceModel> Choices { get; set; } } public class QuestionChoiceModel { public int ChoiceID { get; set; } public string ChoiceText { get; set; } } I'm using EF 5 for this ASP.Net MVC application. Generic Repository Pattern and

Eager loading property of derived class using Include

我是研究僧i 提交于 2020-01-09 10:03:31
问题 I have classes like: Person { Name Address } Employee : Person { Compensation - object } Visitor : Person { } If I write linq: var persons = Context.Persons .Include("Compensation"); I get error: A specified Include path is not valid. The EntityType 'Person' does not declare a navigation property with the name 'Compensation'. It works ok if I do: var persons = Context.Persons .OfType<Employee>() .Include("Compensation"); But I would like to get Employees and visitors in the same query. Looks

Eager loading property of derived class using Include

北慕城南 提交于 2020-01-09 10:03:13
问题 I have classes like: Person { Name Address } Employee : Person { Compensation - object } Visitor : Person { } If I write linq: var persons = Context.Persons .Include("Compensation"); I get error: A specified Include path is not valid. The EntityType 'Person' does not declare a navigation property with the name 'Compensation'. It works ok if I do: var persons = Context.Persons .OfType<Employee>() .Include("Compensation"); But I would like to get Employees and visitors in the same query. Looks