ef-code-first

Entity Framework Code First and Connection Strings

泄露秘密 提交于 2019-12-10 14:14:37
问题 I have a small MVC 3 app using Entity Framework Code First and use this connection string for the model: data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Journal.mdf;User Instance=true;Database=MyJournal When I make a change to the model (e.g. add a property), I get as expected The model backing the 'JournalContext' context has changed since the database was created. So, being in development mode, I go ahead and delete Journal.mdf and Journal.ldf. Now when I

code first one-to-one enable cascade delete

大憨熊 提交于 2019-12-10 14:12:57
问题 I have one to one relationship with foreign keys but the Cascade Delete is not enabled for some reason. The sample code is below. public class AppRegistration { public int AppRegistrationId { get; set; } [Required] [StringLength(50)] [Display(Name = "Username")] public string UserName { get; set; } [Required] [StringLength(100)] public string Password { get; set; } [StringLength(20)] public string StudentOrAgent { get; set; } // navigation properties public virtual AppStatus AppStatus { get;

The navigation property 'SenderId' is not a declared property on type 'Conversation'

时光总嘲笑我的痴心妄想 提交于 2019-12-10 13:47:02
问题 When I try to do Update-Database, I get this error: The navigation property 'SenderId' is not a declared property on type 'Conversation'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property. Edit I believe problem is in mapping relations between Conversation and User, because Conversation and User are connected with two one to many relationships i.e. Conversation has two foreign keys pointing to User Here is how User and Conversation are

Entity framework save changes

本秂侑毒 提交于 2019-12-10 13:14:51
问题 Is there a point to save changes after a read only action? The entities are loaded to cache, but nothing changes, should save changes be called before dispose? 回答1: From doc (DbContext.SaveChanges): Saves all changes made in this context to the underlying database. No there is no point in calling SaveChanges if you have not made any changes on your context. You can read more about this in detail here An entity can be in one of five states as defined by the EntityState enumeration. These

Abstract domain model base class when using EntityTypeConfiguration<T>

两盒软妹~` 提交于 2019-12-10 13:13:41
问题 Is there some trick to getting a central mapping of Base object properties? Is there some simple pattern for abstract classes when using EntityTypeConfiguration. ANy tips much appreciated. Im unable to declare a class Public class BaseEntityConfig<T> : EntityTypeConfiguration<T> Similar issues, where i couldnt get the answers to work How to create and use a generic class EntityTypeConfiguration<TEntity> and Dynamic way to Generate EntityTypeConfiguration : The type 'TResult' must be a non

Using navigation properties in entity framework code first

你。 提交于 2019-12-10 13:13:39
问题 Context: Code First, Entity Framework 4.3.1; User ---- Topic, 1 to Many relation; User with public virtual ICollection<Topic> CreatedTopics Navigation Property(Lazy Loading); Topic with public virtual User Creator Navigation Property; DataServiceController : DbDataController<DefaultDbContext> , Web API beta, ASP.NET MVC 4 Beta , Single Page Application; System.Json for Json serialization; Web API Action: public IQueryable<Topic> GetTopics() { // return DbContext.Topics; // OK return DbContext

The model backing the 'DataContext' context has changed since the database was created

依然范特西╮ 提交于 2019-12-10 12:37:56
问题 I am trying to use Code First with Migrations. Even though there are no current changes to my model, I'm getting an exception. When I add a migration, the up and down are empty, but I get a runtime error with the message as follows: An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: The model backing the 'MyDataContext' context has changed since the database was created. Consider using Code First

contains cycles and cannot be serialized if reference tracking is disabled, json.net and webapi

拈花ヽ惹草 提交于 2019-12-10 12:35:18
问题 I'm getting the error: Object graph for type 'System.Collections.Generic.List`1[[Proj.Model.Prom, Proj.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled. Reading about that, seems to be the serializer, but Json.Net claims to be the solution and I've read WebApi and Framework 4.5 has it by default. So Is it coming by default? If so, Why I'm still getting that error? Thanks! Guillermo. EDIT: Adding code

entity framework code first - shared 1-to-many entity

血红的双手。 提交于 2019-12-10 11:57:23
问题 I've been going around the block a couple of times on this, so I'm taking a fresh approach. I would like to figure out if it possible to have a single entity that is on the many-side of multiple 0-to-many relationships. This is what I'm trying to do: A Client has 0-to-many Phones public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ClientId { get; set; } public string Name { get; set; } public virtual ICollection<Phone> Phones { get; set; } } A Business has 0

How to implement Entity Framework Code First join

做~自己de王妃 提交于 2019-12-10 11:46:46
问题 I'm starting to use Entity Framework Code First. Suppose to have such POCO's (ultimately simplified): public class BortStructure { public Guid Id { get; set; } public String Name { get; set; } } public class Slot { public Guid Id { get; set; } public String Name { get; set; } public BortStructure { get; set; } } public class SystemType { public Guid Id { get; set; } public String Name {get; set; } } public class SlotSystemType { public Guid Id { get; set; } public Slot Slot { get; set; }