entity-framework

Entity Framework loading child collection with sort order

扶醉桌前 提交于 2020-01-18 07:20:21
问题 I have two tables a parent and a child table. The child table has a column sortorder (a numeric value). Because of the missing support of the EF to persist a IList inclusive the sort order without exposing the sortorder (see: Entity Framework persisting child collection sort order) my child class has also a property SortOrder, so that i can store the children with the sort order. In contrast to the autor of the referenced question i try to load the children always sorted. So if i load a

System.NotImplementedException in EntityFramework linq query?

余生长醉 提交于 2020-01-17 17:27:09
问题 Code: var query = await _messageGroupRepository.GetAll() .Include(x => x.Users) .Where(x => x.Users.Any(y => y.Id == 1)) .OrderByDescending(x => x.Date) .Take(5) .ToListAsync(); Result: System.NotImplementedException Solution? 回答1: Welcome to StackOverflow. The NotImplementedException is typically used as a placeholder in methods which are not yet implemented (as the name suggests). It should rarely occur with production code and I don't think I have ever seen it in a mature framework or

Entity Framework Code First: 1:0..1 Change Foreign Key Location

北战南征 提交于 2020-01-17 14:51:25
问题 I have a 1-to-0..1 relationship defined in an Entity Framework code first model like this: public class Album { public int AlbumId { get; set; } public int StampId { get; set; } public Stamp Stamp { get; set; } // other properties } public class Stamp { public int StampId { get; set; } public int AlbumId { get; set; } [Required] public Album Album { get; set; } // other properties } So.. an album has 0..1 stamps, a stamp always has exactly one album. The configuration I have here works nicely

Entity Framework tries to create user when adding other object to db

只愿长相守 提交于 2020-01-17 13:56:51
问题 I'm trying to create an address and I get a weird exception like so: Cannot insert the value NULL into column 'LoweredUserName', table 'SkiShop.dbo.aspnet_Users'; column does not allow nulls. INSERT fails. The statement has been terminated. I thought ok, perhaps there's a stray user having been created but not comitted but nope, a db.saveChanges() call before trying to create and add the address goes fine! Here's my code (using ASP.NET MVC 2): var userId = Helpers.CreateAspNetUserFromUser

MVC Identity wants to insert Null into table when value is not Null

*爱你&永不变心* 提交于 2020-01-17 13:52:10
问题 I just recently posted a question regarding adding properties to ApplicationUsers, and now this question follows from that: So now the FirstName and LastName properties for ApplicationUser are fields in the AspNetUsers table. But when I go to register as a new user, I get this error: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'LastName', table 'aspnet-CustomUserProps-20151019035309.dbo.AspNetUsers'; column does not allow nulls. INSERT fails. This is my

MVC Identity wants to insert Null into table when value is not Null

南楼画角 提交于 2020-01-17 13:49:07
问题 I just recently posted a question regarding adding properties to ApplicationUsers, and now this question follows from that: So now the FirstName and LastName properties for ApplicationUser are fields in the AspNetUsers table. But when I go to register as a new user, I get this error: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'LastName', table 'aspnet-CustomUserProps-20151019035309.dbo.AspNetUsers'; column does not allow nulls. INSERT fails. This is my

OptimisticConcurrencyException when persisting related entry

夙愿已清 提交于 2020-01-17 12:36:08
问题 FIXED I need to read the documentation to see why it's fix the error, but adding [DatabaseGenerated(DatabaseGeneratedOption.Identity)] before each key that is AUTO INCREMENT fix the problem. . sorry about that, i read allot of questions like this I am doing, but i can't figure out what's happening... I'm making only one request, so i'ts not concurrency ... I have 3 entitys FIRST: [Table("banco_bar.mesa")] public partial class mesa { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft

EF 4.3 CF does not update relationships when saving changes

Deadly 提交于 2020-01-17 12:27:09
问题 I've got a ClubItem object with a relationship collection ICollection ClubUsers. I load a ClubItem from the context and add a couple of new users to the CluItem. When saving the ClubItem in a disconnected state the context does not see the new ClubUsers as new entities. How to tell the context that something changed? I allready use this to change entitystate of the ClubItem: public virtual void Update(IEntity entityToUpdate) { DbSet.Attach(entityToUpdate); Context.Entry(entityToUpdate).State

MVC Many to Many View

邮差的信 提交于 2020-01-17 10:00:22
问题 Again I have problem with my project. I have two models created with EF 5 DBContext Generator: First: public int ID_AN { get; set; } public string TITLE_OR { get; set; } public string TITLE_EN { get; set; } public virtual ICollection<GENRES> GENRES { get; set; } Second: public int ID_GE { get; set; } public string GENRE { get; set; } public virtual ICollection<ANIME> ANIME { get; set; } After that I created controller: public ActionResult Details(int id) { using (var db = new

MVC Many to Many View

做~自己de王妃 提交于 2020-01-17 09:59:26
问题 Again I have problem with my project. I have two models created with EF 5 DBContext Generator: First: public int ID_AN { get; set; } public string TITLE_OR { get; set; } public string TITLE_EN { get; set; } public virtual ICollection<GENRES> GENRES { get; set; } Second: public int ID_GE { get; set; } public string GENRE { get; set; } public virtual ICollection<ANIME> ANIME { get; set; } After that I created controller: public ActionResult Details(int id) { using (var db = new