entity-framework-4.1

Entity Framework: Tracking changes to FK associations

Deadly 提交于 2019-12-18 01:21:32
问题 I'm overriding SaveChanges on my DbContext in order to implement an audit log. Working with many-to-many relationships or independent associations is relatively easy as EF creates ObjectStateEntries for any changes to those kinds of relationships. I am using foreign key associations, and when a relationship between entities changes all you get is an ObjectStateEnty that says for example entity "Title" has "PublisherID" property changed. To a human this is obviously a foreign key in Title

Entity Framework cant use the DbContext, model being created

江枫思渺然 提交于 2019-12-17 20:57:30
问题 I am using EF 4.1, and I create a normal EF edmx file. I generate it from a DB. When it's been generated I rightclick and select add code generation item, to generate new classes, and use the DbContext instead. I use the template DbContext generator. Everything works fine. Then I trie to query the context: using (var context = new PasDBEntities()) { var client=context.ClientCompanies.SingleOrDefault(_=>_.ID==clientCompanyId); if(client!=null) I have no problem creating a new instance of the

Guidance for synchronising reverse associations in Entity Framework 4.1

≯℡__Kan透↙ 提交于 2019-12-17 18:58:09
问题 EF 4.1 synchronises reverse associations when you create your instances. Is there any documentation of or best practices guidance available for this behaviour? What I mean by synchronising the reverse association is that given: public class Blog { public Blog() { Posts = new List<Blog>(); } public int Id { get; set; } public ICollection<Post> Posts { get; private set; } } public class Post { public Blog Blog { get; set; } public int Id { get; set; } } Then after the following line the Post

How can I prevent EF “The context cannot be used while the model is being created” errors?

安稳与你 提交于 2019-12-17 18:22:04
问题 Looking at my Elmah error logs, I am seeing a few InvalidOperationException s from Entity Framework that deal with: The context cannot be used while the model is being created. This is with the latest EF CodeFirst library from Nuget. The only information I have been able to find on the net is that it is being caused by having data contexts as singletons, which is most certainly not my case. In my Windsor installer, my EF unit of work structure is being registered with: container.Register

Readonly properties in EF 4.1

泄露秘密 提交于 2019-12-17 17:46:15
问题 I've faced with situation when I need to have EF readonly property in case of 'optimistic update'(you do not load current state of your domain object from database to check what properties are really changed. You just set your object as Modified and update it to database. You avoid redundant select and merge operations in this case). You can't write something like this : DataContext.Entry(entity).Property(propertyName).IsModified = false; , because setting of 'false' value is not supported

Entity Framework Code First - Defining Relationships/Keys

坚强是说给别人听的谎言 提交于 2019-12-17 17:26:36
问题 I am designing my database using code first and I need a little help I think. I am getting this error: Introducing FOREIGN KEY constraint 'SalesOrder_Invoices' on table 'Invoices' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors. I am trying to have the following relationships/keys: --> = 1 to Many Relationship Customer --> CustomerLocation CustomerLocation

Entity Framework DbContext SaveChanges() OriginalValue Incorrect

匆匆过客 提交于 2019-12-17 14:03:46
问题 I am trying to implement an AuditLog using EF 4.1, by overriding the SaveChanges() method as discussed in the following places: http://jmdority.wordpress.com/2011/07/20/using-entity-framework-4-1-dbcontext-change-tracking-for-audit-logging/ Entity Framework 4.1 DbContext Override SaveChanges to Audit Property Change I am having problems with the "modified" entries though. Whenever I attempt to get at the OriginalValue of the property in question, it always has the same value as it does in the

How do I precompile an Entity Framework Code-First Query?

非 Y 不嫁゛ 提交于 2019-12-17 12:49:39
问题 I am encountering some performance problems with my Entity Framework Code-First queries and I believe that precompilation may be the answer. If I were using "normal" Entity Framework, I would simply use the CompiledQuery.Compile method to precomiple my queries. But since I have a DbContext and not an ObjectContext, I can't get this to work. I do realize that DbContext is an IObjectContextAdapter, which gives me access to the ObjectContext, but I cannot find the method that lets me get an

Entity Framework 4.1 DbSet Reload

泪湿孤枕 提交于 2019-12-17 11:45:12
问题 I'm using a single instance of DbContext scenario to shadow entire copy of the database locally in a WPF app. I've heard this is bad practice, but my database is small and I need an entire copy of it locally while the app is running. An extension method for IQueryable , Load() lets me preload the elements of a DbSet<> , so that I can bind things to the Local property of DbSet<> . Data in the database changes rapidly, so I want to SaveChanges() and reload everything , even objects that are

What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

落爺英雄遲暮 提交于 2019-12-17 10:58:45
问题 In EF 4.1+, is there a difference between these 2 lines of code? dbContext.SomeEntitySet.Add(entityInstance); dbContext.Entry(entityInstance).State = EntityState.Added; Or do they do the same thing? I'm wondering if one might affect child collections / navigation properties differently than the other. 回答1: When you use dbContext.SomeEntitySet.Add(entityInstance); the status for this and all its related entities/collections is set to added, while dbContext.Entry(entityInstance).State =