entity-framework-4

Is it possible to call named method within a call to Where?

让人想犯罪 __ 提交于 2020-01-14 09:11:30
问题 I am trying to understand some performance implication of Linq from this free ebook by RedGate ftp://support.red-gate.com/ebooks/under-the-hood-of-net-memory-management-part1.pdf On page 157-158 in this book, they created following example. Order[] pastDueAccounts = null; DateTimedueDate = DateTime.Today.AddDays(-7); using(varcontext = new Context()) { pastDueAccounts = context.Accounts.Where(account => account.DueDate < dueDate).ToArray(); } They then re-factored part of lamda expression

EF 4: Problems understanding DetectChanges when using POCO (no self tracking ObjectContext)

荒凉一梦 提交于 2020-01-14 05:21:27
问题 I wonder if anyone can help me? I am having problems understanding why i need to issues DetectChanges on my POCO (non proxy) entities. Of course i have this line to ensure that proxies are not returned. context.ObjectStateManager.GetObjectStateEntry(order).State And doing some research it appears if i need to check the "state" of an object then i need to issue detechChanges But why would i need to check the State of an object? Basically I send along my POCO entity to a method that SAVES the

Configure Custom MetadataProvider for ViewModel

爱⌒轻易说出口 提交于 2020-01-14 04:21:26
问题 I have a ViewModel for which want to be able to set Metadata properties dynamically. For example I would like to be able to customise the DisplayName and validation error messages using the value of other model properties. I plan to do this with a custom MetadataProvider, following Brad Wilson's article. I want the provider only to be used with selected ViewModels. So my question is, how do I configure that? I have seen examples using ModelMetadataProviders.Current = new

Entity Framework 4 - TPT Inheritance in Features CTP5 (code first): rename foreign key column on inherited table

一曲冷凌霜 提交于 2020-01-14 03:38:07
问题 I'm trying to convert an xml Entity Framework model to Code First (CTP5) one. I have to model a hierarchy which fits quite well the TPT pattern. The only problem I have is that the primary key/foreign key of the "inheriting" table has a different name from the primary key of the base class. These are the relevant fields of the involved tables CREATE TABLE site.Domains ( ID INT NOT NULL PRIMARY KEY, Domain NVARCHAR(128) NOT NULL ) CREATE TABLE site.MainSites ( FKDomainID INT NOT NULL PRIMARY

Severe performance problems when performing a simple query using Entity Framework

若如初见. 提交于 2020-01-14 03:13:08
问题 I've got a fairly generic CRUD webapp, which generates pages dynamically according to the contents of several database tables. I'm using Entity Framework 4.0 to pull this data out of the DB, however I'm running into severe performance problems. I've managed to iterate down into a problem which is contained enough that I can detail below. I have a table containing list of Page Forms (~200). Each form has one or more Fields (~4000 total), and each field has may have some Parameters (~16000

DbContext crashes with PrimitiveType != null error

无人久伴 提交于 2020-01-13 10:29:06
问题 Using Entity Framework Code First, the web application crashes on a call to DbContext with the following error: Assertion failed Expression: primitiveType != null Description: Assertion failed: primitiveType != null It crashes on the following line of code: public class MyDb : DbContext { which is called by: MyDb _db = new MyDb(); So it seams like calling DbContext generates a fatal error. DbContext is an EF function and I cannot debug inside EntityFramework.dll 回答1: It's a problem related to

Getting 3 random records from a table

吃可爱长大的小学妹 提交于 2020-01-13 09:14:37
问题 I've read multiple answers to a similar query, but none seem to hit the spot. Imagine I have a table that contains 10 rows, how do I retrieve 3 random rows from this table using Entity Framework? Not just 1 random row, but 3 random rows - each being different from the other? Thanks in advance 回答1: var threeRandomFoos = foos.OrderBy(x => Guid.NewGuid()).Take(3); 回答2: Instead, there is a simpler way, var threeRandomFoos = foos.OrderBy( x=> SqlFunctions.Rand()).Take(3); Guid.NewGuid will be

Getting 3 random records from a table

試著忘記壹切 提交于 2020-01-13 09:13:51
问题 I've read multiple answers to a similar query, but none seem to hit the spot. Imagine I have a table that contains 10 rows, how do I retrieve 3 random rows from this table using Entity Framework? Not just 1 random row, but 3 random rows - each being different from the other? Thanks in advance 回答1: var threeRandomFoos = foos.OrderBy(x => Guid.NewGuid()).Take(3); 回答2: Instead, there is a simpler way, var threeRandomFoos = foos.OrderBy( x=> SqlFunctions.Rand()).Take(3); Guid.NewGuid will be

Querying Many to Many and Conditional Where

限于喜欢 提交于 2020-01-12 07:54:16
问题 Within my Context file, I set up a many to many relationship between my Location class and Program class. protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Location>() .HasMany(u => u.Programs) .WithMany(r => r.Locations) .Map(m => { m.ToTable("LocationsPrograms"); m.MapLeftKey("LocationId"); m.MapRightKey("ProgramId"); }); } I'm creating a search/filter form where the user will need to be able to filter the locations by selecting a program. My thought

How Do I Mock Entity Framework's Navigational Property Intelligence?

时光怂恿深爱的人放手 提交于 2020-01-12 05:57:25
问题 I'm attempting to test my repository using an in-memory mock context. I implement this with an in-memory Dictionary, as most people do. This implements members on my repository interface to Add, Remove, Find, etc, working with the in-memory collection. This works fine in most scenarios: [TestMethod] public void CanAddPost() { IRepository<Post> repo = new MockRepository<Post>(); repo.Add(new Post { Title = "foo" }); var postJustAdded = repo.Find(t => t.Title == "foo").SingleOrDefault(); Assert