entity-framework-6

Proper method of navigation to generically-defined entity

纵饮孤独 提交于 2019-12-08 17:02:37
问题 New bounty 2017/10/31 The auto-accepted answer, unfortunately, does not work with my current entity model due to TPC limitations. I am in dire need of finding a way to facilitate two-way navigation via interface or abstract class, so I am starting another bounty. Note that I must use existing model design, so refactoring is not an option. Original question below I have an parent entity that has a one-to-one relationship to multiple possible tables (FK is on child tables). Because the

Entity Framework Code-First - Define the Non-clustered key for this EntityType

百般思念 提交于 2019-12-08 13:42:51
问题 I Have defined an entity class with a key, but I don,t want that key be cluster because I have another cluster index in my entity, but I received an error when I add an Index attribute to my model, Can anybody tell me that how can I define a Non-Clustered Key ???? 回答1: AFAIK it is still not possible to create non-clustered primary key fields as part of your code first configuration. However, if you are using migrations or are in a position where you are able to, see the following Entity

Prevent EF from adding new child objects instead of just association when inserting new parent object?

心不动则不痛 提交于 2019-12-08 12:30:31
问题 I have the following: public int CreateExercise(Exercise newExercise) { db.Exercises.Add(newExercise); db.SaveChanges(); return newExercise.ExerciseId; } Where Exercise is the following: public class Exercise { public Exercise() { Users = new Collection<User>(); } [Key] [Required] public int ExerciseId { get; set; } [StringLength(300, ErrorMessage = "The value cannot exceed 300 characters. ")] public string Title { get; set; } public virtual ICollection<User> Users{ get; set; } } When I

After setting EntityState.Unchanged the entity properties are lost

半城伤御伤魂 提交于 2019-12-08 11:42:54
问题 I have an ASP.Net MVC project where I add in a View a student entity to my Entity Framework 6 repository. class Student public long Id { get; set; } public virtual Book Favorite { get; set; } class Book public long Id { get; set; } public string Title { get; set; } The Favorite property is set from a drop down in the view. In the controller on post, only the Id for the book is set. To tell Entity Framework, to only add the student, but not to add the referenced book, I set the EntityState for

Dependencies error after changing data type in Entity Framework?

大城市里の小女人 提交于 2019-12-08 11:31:35
问题 I've changed 2 columns type by specifying the attribute [Column(TypeName = "date")] on the properties: public DateTime LastModified { get; set; } = DateTime.Now; public DateTime OriginalCreated { get; set; } = DateTime.Now; When I run the application (MVC) I get an error: Additional information: The object 'DF__StoryMode__Origi__25869641' is dependent on column 'OriginalCreated'. ALTER TABLE ALTER COLUMN OriginalCreated failed because one or more objects access this column. I followed this

Using a entity framework 5 project dll with Web project using EF6

流过昼夜 提交于 2019-12-08 08:56:13
问题 I have a logging solution that uses EF5 which I am referencing in a new a new web application that uses EF 6. I am getting the following error when consuming the anything from that logging dll: Method not found: 'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()' I know why I am getting the error, it's because namespaces have changed from EF5 to EF6, but is there a way I can have the two co-exist without having to change my EF5

Repository pattern with EF6 + DatabaseFirst

 ̄綄美尐妖づ 提交于 2019-12-08 08:36:31
问题 Current System: I am working on a project which is multi layered as shown below (in order of flow) and I am learning and trying to implement Repo Pattern with UOW on EF Database first. Service (Web API) Business (C# Class Library) Repository (Repo Pattern + UOW) ViewModels (Used by my Service for sent to my UI layer) Data (Entities) Database (SQL Server) Repository: Generic Repository: public interface IRepository<TEntity> where TEntity : class { void Create(TEntity entity); IQueryable

Display Image from Database Using View Models MVC 5

限于喜欢 提交于 2019-12-08 07:00:49
问题 Am working with the built-in template of visual studio 2013 mvc 5. am trying to display images i saved in the database to the index view of the home page. the yellow highlighted sections are where i want to display the images this is my view model for the images i want to display from the database using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace HaveYouSeenMeApp.Models.ViewModels { public class ImageViewModel { public string PetPhotoName { get;

EF6: How to avoid circular reference?

雨燕双飞 提交于 2019-12-08 06:55:03
问题 What are the possibilities to avoid circular reference with Entity Framework 6 during JSON serialization with ASP.NET Web API? I generated a edmx (Entity Data Model) file for Entity Framework 6, database first. I try to build an API with ASP.NET Web API. When I try to return my JSON object in my controllers I get a runtime exception of serialization because of circular reference. Indeed when I double check my database and my entities I see one of my entity contains a list another entity that

Entity Framework - The migrations configuration type was not be found in the assembly

廉价感情. 提交于 2019-12-08 06:36:06
问题 I have multiple dbcontexts in a C# project and I'm trying to enable migrations, when I specify the full command ie: "Enable-Migrations -ContextTypeName Models.Account.AccountDetailDbContext" A migrations folder is created, with the configuration class I then get a message: "Checking if the context targets an existing database..." And then "The migrations configuration type 'Portal.WebUI.Migrations.Configuration' was not be found in the assembly 'Portal.WebUI'." Even though it has just created