entity-framework-4.1

Attaching and detaching entities from context correctly in EF4.1

独自空忆成欢 提交于 2019-11-27 05:28:32
问题 I am trying to implement caching mechanism for entities. And to use the entities correctly and seamlessly with the caching i need to detach the entity from the current context before i put it in a cache and attach it back the the new context when i get it from the cache. (My context lifetime is per http request) The requirements are that - All the navigational properties that are associated with it (which i have already populated) should not be removed when the entity is detached. I can

Entity Framework Code First : Setting up One-To-One foreign key association using Annotations

你。 提交于 2019-11-27 05:02:37
I have following two Entities that I am trying to relate (one to one) using foreign key associations. public class StandardRack { public int Id {get;set} public StandardRelay StandardRelay {get;set} } public class StandardRelay { public int Id {get;set} public int StandardRack_Id {get;set;} [Required][ForeignKey("StandardRack_Id")] public StandardRack StandardRack { get; set; } } This throws ModelValidationException. Any ideas why such a seemingly simple one-to-one bidirectional relationship cannot be configured. Edit: Here is the Exception: System.Data.Entity.ModelConfiguration

How do I define a database view using Entity Framework 4 Code-First?

六眼飞鱼酱① 提交于 2019-11-27 04:48:49
How do I define a database view using Entity Framework 4 Code-First? I can't find anything about this anywhere! Ladislav Mrnka That's because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can't define such constructs using code first. If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer - it will be similar like this answer . Just be aware that this will not help you if you want to map entity to a view. In such case you

'ObjectContext' does not contain a definition for 'Entry' and no extension method 'Entry'

陌路散爱 提交于 2019-11-27 04:35:59
问题 I upgraded my EntityModel to version 4.3 using NuGet . Now i want to change my EntityObject.State , but it cant find the .Entry() method. The current state is Deleted . This is what i want to do: someObjectContext.Entry(someEntityObject).State = EntityState.Unchanged; The referances to EntityFramework and EntityFramework.Extended are added. So, what am i missing? EDIT My NuGet output: PM> Install-Package EntityFramework -Version 4.3.1 'EntityFramework 4.3.1' already installed. Data already

Entity Framework 4.1 The model backing the context has changed since the database was created, immediately after creating DB

偶尔善良 提交于 2019-11-27 04:32:33
问题 I am working on a project which uses Entity Framework 4.1 for persisting our various objects to the database (code first). I am testing in Visual Studio with a local SQL Express DB, and our Jenkins server deploys committed code to a testing server. When this happens I temporarily change my local connection string to point to the testing DB server and run a unit test to re-create the test database so that it matches our latest entities, etc. I've recently noticed our testing server is giving

Entity Framework 4.1 default eager loading

跟風遠走 提交于 2019-11-27 04:26:05
I'm using Entity Framework 4.1 code first approach. I want to make eager loading as my the dafault configuration, and by that avoid using the Include extension method in each fetching query. I did as recomended in MSDN, changing the simple lazy property at the DbContext constructor: public class EMarketContext : DbContext { public EMarketContext() { // Change the default lazy loading to eager loading this.Configuration.LazyLoadingEnabled = false; } } unfortunately, this approach is not working. I have to use the Include method to perform eager loading in each query. Any ideas why? Thanks in

Ramifications of DbSet.Create versus new Entity()

允我心安 提交于 2019-11-27 04:20:23
I am a bit confused about whether to use DbSet.Create, or simply new up an entity and add it. I don't really understand the ramifications of using DbSet.Create. I understand that DbSet.Create will create a proxied version if applicable, but I don't really understand what that means. Why do I care? It seems to me that an empty Proxied class is no more useful than a non-proxied class, since there are no related entities to lazy load. Can you tell me the difference, beyond the obvious? And why would you care? Slauma A scenario where using DbSet<T>.Create() makes sense is attaching an existing

Getting exact error type in from DbValidationException

扶醉桌前 提交于 2019-11-27 04:08:59
问题 I have the situation where I'm initializing my model in DatabaseInitializer() for EF 4.1 and get this annoying error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." So, I go to this EntityValidationErrors and there is a field {System.Data.Entity.Validation.DbEntityValidationResult} which gives me no information at all about what field it was unable to initialize. Is there a way to get more info about this error? To clear things out: I know

Enums with EF code-first - standard method to seeding DB and then using?

久未见 提交于 2019-11-27 04:00:05
问题 Is there a standard way to using Enums in EF code-first? There seems to be some examples making use of a wrapper class for the enum. However, I would like to be able to define the enum and have the enum values also seeded into the database using the database initializer. There doesn't seem to be much point in defining the enum and creating a wrapper, if I then have to seed the database table manually from the enum. 回答1: Now supported : http://blogs.msdn.com/b/adonet/archive/2011/06/30

Navigation Property without Declaring Foreign Key

こ雲淡風輕ζ 提交于 2019-11-27 03:43:09
All my models contain at least two associations. When modeling this in ef4 I've only been able to do this without a second Foreign Key property through the use of the fluent interface. ForeignKey seems like the right attribute to use, except for the fact that it requires a string parameter. So my question is, can you have a navigational property and declare it as such using an attribute? public class User : IAuditable { // other code public virtual User Creator { get; set; } public virtual User Modifier { get; set; } } I believe, it is not possible to define the relationship only with data