entity-framework-4.1

Entity Framework Change Tracking API and reference entries

感情迁移 提交于 2019-12-05 04:34:57
Looking to write generic Audit code on my DbContext subclass. foreach (var entry in this.ChangeTracker.Entries<MyClass>()) { if (entry.State == EntityState.Modified) { var entityProperties = entry.Entity.GetType().GetProperties(); foreach (var entityProperty in entityProperties) { DbMemberEntry propertyEntry = entry.Member(property.Name); if (propertyEntry is DbPropertyEntry) { // IsModified available } else if (propertyEntry is DbReferenceEntry) { // IsModified not available } } } } 1) If I only change a reference property, the entry.State value is "Unchanged". 2) Even if point 1 was set to

How to use Order By in this MSDN example

不想你离开。 提交于 2019-12-05 02:26:17
I'm trying to figure out how to use this orderBy parameter. I am not sure what I am suppose to pass in. http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[

Reusing LINQ query results in another LINQ query without re-querying the database

こ雲淡風輕ζ 提交于 2019-12-05 02:19:26
问题 I have a situation where my application constructs a dynamic LINQ query using PredicateBuilder based on user-specified filter criteria ( aside: check out this link for the best EF PredicateBuilder implementation ). The problem is that this query usually takes a long time to run and I need the results of this query to perform other queries (i.e., joining the results with other tables). If I were writing T-SQL, I'd put the results of the first query into a temporary table or a table variable

Entity Framework attach trouble in many to many update scenario

老子叫甜甜 提交于 2019-12-05 02:00:11
问题 I have a scenario where I wish to update a movie entity and its many to many relationship with Genres. The navigation property Genres in movie contains stub Genre objects that only contain the GenreID because I want to save querying the DB for all genres. See the code below, its fairly self explanatory. The problem is that I need to attach my "Stub" genres to the context so the EF will only modify the M:M relationship and not try to create new Genre records. This causes an error if a genre is

Entity Framework Code First - One-to-Many with a join/link table

£可爱£侵袭症+ 提交于 2019-12-05 01:39:19
问题 Is it possible to create a One-to-Many relationship with Code First that uses a link/join table between them? public class Foo { public int FooId { get; set; } // ... public int? BarId { get; set; } public virtual Bar Bar { get; set; } } public class Bar { public int BarId { get; set; } // ... public virtual ICollection<Foo> Foos { get; set; } } I want this to map as follows: TABLE Foo FooId INT PRIMARY KEY ... TABLE Bar BarId INT PRIMARY KEY TABLE FooBar FooId INT PRIMARY KEY / FOREIGN KEY

How to build a dynamic FROM clause for a LINQ query?

こ雲淡風輕ζ 提交于 2019-12-05 01:27:04
问题 I have a standard LINQ query: var list = from x in SomeDataContext.ViewName where //Rest of where clause select x; I would like to know if it is possible to build a dynamic LINQ query so that i can change the SomeDataContext.ViewName at runtime. I have about 5 different views, all with the basic columns needed to perform the where clause, but with some different column names for each of other views. So is it possible to build up the query so that i can use the different context at runtime,

Populating child property with Entity Framework SqlQuery

杀马特。学长 韩版系。学妹 提交于 2019-12-05 00:41:32
Given the following POCO Code First Entities public class Customer { public int CustomerId { get; set; } public string CustomerTitle { get; set; } public string CustomerFirstName { get; set; } public string CustomerLastName { get; set; } public ICollection<Order> Orders { get; set; } } public class Order { public int OrderId { get; set; } ... public int CustomerId { get; set; } public Customer Customer { get; set; } } Using linq you can get the Orders filled in by using the Include property as in var cust = (from cust in context.Customer where cust.CustomerId == 1 select cust) .Include(ord =>

Entity Framework 4.1 code-first KeyAttribute as non-identity column

ぐ巨炮叔叔 提交于 2019-12-05 00:15:17
I've got a problem with a code-first model I've got. Data is now in the database so I can't re-seed the database using a DropCreateDatabaseIfModelChanges class, but I need to change one table so that a bigint column is not an IDENTITY(1,1). I've managed to do this using SSMS but now my EF code is saying it's out of date. This is the code for the table in question: public class Vote { [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public long FacebookUserId { get; set; } [Required] public Entity Entity { get; set; } } So I've changed my table schema, and my model (which I

DbContext's ChangeTracker problem

本秂侑毒 提交于 2019-12-04 22:55:51
I have a codefirst EF-4.1 based program. The user gets a context and can modify some properties. When the user is done, I do a quick ChangeTracker.Entries().Any(e => e.State != EntityState.Unchanged); to determine if a SaveChanges() is required or not. If I do a 'SaveChanges()' call, the changes that have made are persisted to the database. This works for some properties, and doesn't work for others. Specifically it seems to work with simple types ( float s), and with collection hierarchies( ObservableCollection s). Am I doing something wrong? Ladislav Mrnka Yes this is a problem. Some

Reload field value modified in DB by trigger after Insert/Update

ⅰ亾dé卋堺 提交于 2019-12-04 19:41:34
I have an entity with a string property mapped to a nvarchar field in the DB. I use an after insert/update trigger to set the value of this field. By default EF will not load the value of this field after insert/update - only identity fields are reloaded from DB after insert. I've tried to change the StoreGeneratedPattern option on this field to Computed (which seems like the right way to do that) but I get the error: The store generated pattern 'Computed' is not supported for properties that are not of type 'timestamp' or 'rowversion'. Does it mean that EF supports reloading only timestamps?