entity-framework-4

Entity Framework ObjectQuery order by date on many to many relation

烈酒焚心 提交于 2021-01-28 05:48:02
问题 I have an ObjectQuery like that: ObjectQuery<Car> query = GetCarQuery(); than i want to order by property Date on related entity Race , somehting like query = (ObjectQuery<Car>)query.OrderBy(x=> x.Races.Date); query.Skip((page - 1) * rows).Take(rows).ToList(); was trying to go like that: query = (ObjectQuery<Car>)query.OrderBy(i => i.Races.OrderBy(x=> x.Date)); query.Skip((page - 1) * rows).Take(rows).ToList(); //error here and got an error when query executed DbSortClause expressions must

The type 'X' is not mapped as a Table

烈酒焚心 提交于 2021-01-28 04:04:31
问题 I have ADO.NET Entity Data Model for my tables and if I use it directly it works fine. I wanted to write a wrapper, so that I don't have to repeat the same code again. Ended up writing some DataContext Extension methods. So, have something like this to get Data from the Table. public static TEntity Get<TEntity>(this DataContext dataContext, object id, string primaryKeyName) where TEntity : class, new() { if (id == null) return new TEntity(); var table = dataContext.GetTable<TEntity>(); return

The column name is not valid

喜欢而已 提交于 2021-01-27 13:42:59
问题 I get a common error that probably is easy to solve but I have no clue how. This is the message I get. The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if

Using only the year part of a date for a WHERE condition

冷暖自知 提交于 2021-01-26 20:34:48
问题 In the LINQ statement below, I want to select people with an exam date in 2010. The exam date is stored as a datetime as the actual date and time is used in other applications. What is the most elegant, easiest, best way to compare the exzam date to only '2010'. Or, should I just compare, using >=, the exam date to 1/1/2010? var active = dc.People.Where(x => x.exam >= 2010) .Select(x => new {x.ContactID, x.FirstName, x.LastName}) ); x.MostRecent == DateTime.Parse("1/1/2010").Year EDIT #1 I

Using only the year part of a date for a WHERE condition

ε祈祈猫儿з 提交于 2021-01-26 20:34:41
问题 In the LINQ statement below, I want to select people with an exam date in 2010. The exam date is stored as a datetime as the actual date and time is used in other applications. What is the most elegant, easiest, best way to compare the exzam date to only '2010'. Or, should I just compare, using >=, the exam date to 1/1/2010? var active = dc.People.Where(x => x.exam >= 2010) .Select(x => new {x.ContactID, x.FirstName, x.LastName}) ); x.MostRecent == DateTime.Parse("1/1/2010").Year EDIT #1 I

1-to-1 relationship causing exception: AssociationSet is in the 'Deleted' state. Given multiplicity constraints

北城余情 提交于 2020-07-18 04:16:39
问题 I have set up a 1-to-1 relationship using EF code first following the method prescribed here: Unidirectional One-To-One relationship in Entity Framework My mapping looks like this ... protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Asset>() .HasRequired(i => i.NewsItem) .WithOptional(e => e.Asset) .Map(m => m.MapKey("NewsItemId")); } But when I get this exception ... A relationship from the 'Asset_NewsItem' AssociationSet is in the 'Deleted' state.

Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship

馋奶兔 提交于 2020-07-06 09:06:56
问题 I have an entity with a foreign key relationship to an asp.net membership provider users table. This portion of the model looks like this: I can't seem to assign the foreign key relationship when inserting the Users table record, the record containing the foreign key to the aspnet_Users table. I keep getting the error: An item with the same key has already been added. The code I'm using looks like: UserAdmin userToAdd = new UserAdmin(); ... userToAdd.aspnet_Users = membershipUser; //line

EF migration shows empty Up() Down() methods, no tables created in database

泄露秘密 提交于 2020-06-27 06:03:53
问题 EF migration shows empty Up() Down() methods _migrationhistory created in database but no tables has been created code for creating model Public partial class Student:BaseEntity { public string name { get; set; } public string collegeName { get; set; } public int numberOfBooks { get; set; } } code for model mapping public partial class StudentMap: EntityTypeConfiguration<Student> { public StudentMap() { this.ToTable("Students"); this.HasKey(c => c.Id); this.Property(c => c.name); this

How to update an entity without a round-trip? (EF 4)

烈酒焚心 提交于 2020-03-18 12:12:33
问题 I tried the following: public void UpdatePlayer(int id) { Player player = new Player() {ID = id}; player.Password = "12"; Entities.Players.Attach(player); Entities.SaveChanges(); } No change at the db. What am I missing? 回答1: I think it might be because you're setting the values before you attach the object - the data context will not know what fields have changed. Try: public void UpdatePlayer(int id) { Player player = new Player() {ID = id}; Entities.Players.Attach(player); player.Password

“Ambiguity between X and X” for every property after modifying entity model [closed]

大憨熊 提交于 2020-02-23 09:33:49
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm getting "Ambiguity between 'XYZ.EntityX.PropertyX' and 'XYZ.EntityX.PropertyX'" errors on every reference to properties in my entity model after making a small change to it and attempting to recompile. Both properties are identical in every case. There are also a lot of "The type 'XYZ.EntityX' already