entity-framework-4

Why am I getting error “The property X on type Y cannot be set because the collection is already set to an EntityCollection”?

别来无恙 提交于 2020-01-04 02:49:08
问题 While I was trying to map a collection to another in EF4 I got this error. The property 'ResourceLanguages' on type 'Resource_EF810770B4FCA2E071F38C2F2EE328AAC216CA2A7BF157503E6658A42D7CF53A' cannot be set because the collection is already set to an EntityCollection. I was trying to code like this foreach (var resource in resources) { resourceLanguages = resourceLanguageRepositoty.GetAllByResourceId(resource.Id); resource.ResourceLanguages = resourceLanguages; } Can anyone help me to sort

ADO.Net Entity Framework generate model from XSD file

风流意气都作罢 提交于 2020-01-04 02:41:32
问题 I have a rather large XSD file and I have heard that it is possible using the entity framework to connect to XML files like databases and validate them against the XSD, however I do not know how to go about doing this. So what I'm trying to do is generate a entity framework model from my XSD file, and I imagine this will generate a series of objects which will be used when opening a XML file for validation and population. 回答1: Linq2xsd will do that. http://linqtoxsd.codeplex.com/ once dead it

Aggregating multiple queries into one with EF like this?

ぐ巨炮叔叔 提交于 2020-01-04 02:30:54
问题 I am using EF 4.0 POCO in my application. Are there any downsides to retrieving information like this? Given a customerId and a productId , I would like to apply some business rules that require me to fetch lots teeny pieces of information from the database requiring multiple queries. Instead, I could write one query like so: var customerId = 1; var productId = 1; var aggregateQuery = from entry in Customers.Take(0).DefaultIfEmpty() select new { numberOfOrders = SalesOrderHeaders.Where

The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object

戏子无情 提交于 2020-01-04 01:57:15
问题 I thought I understood EF, particularly in terms of excrutiatingly simple CRUD, but I may be wrong. I have an ObjectContext/Repository pattern, and in this case a VatCode entity which has a collection of VatRates. VatCode 1--* VatRate I want to provide the functionality for CRUDing these entities. This is in a WPF application, and is operating "disconnected" in that I am not holding DB connections open unnecassarily. I prefer the in/out method of database access. This is shown in the

Entity Framework: Type “mapped as a complex type” Error

时间秒杀一切 提交于 2020-01-04 01:12:54
问题 In my database, I have a table called "tracking" with the following columns: [OrderNumber] [varchar](50) NULL, [TrackingNumber] [varchar](50) NULL, [emailaddress] [varchar](100) NULL, [courier] [varchar](10) NULL I have a class called Tracking to represent entities from the table: public class Tracking { public virtual string OrderNumber { get; set; } public virtual string TrackingNumber { get; set; } public virtual string EmailAddress { get; set; } public virtual string Courier { get; set; }

Nullable optional parameter

亡梦爱人 提交于 2020-01-03 19:43:43
问题 I am using the entity framework 4 with edmx files and POCOs within an asp.net mvc application. First of all I have a person class which is mapped to a table in the database. public class Person { public Int32 ID{get;set;} public string Name{get;set;} public Int32? ParentID{get;set;} } Then in my service layer I have the following function to retrieve all persons. If a parentID is supplied the persons retrieved will be the ones with that parentID: public List<Person> Get(int? parentPersonID =

How go get max value of a unique id column using LINQ

↘锁芯ラ 提交于 2020-01-03 15:32:29
问题 How can I write this in the simplest way using LINQ? SELECT MAX(Game_id) AS MaxValue FROM Dim_Game 回答1: Try context.Dim_Games.Max(g => g.Game_id); 回答2: If your column is not nullable and result of you query is empty, you will receive the error "The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type." To avoid the error you should cast column to nullable and result coalesce with 0.

EF eqivalent for rows affected of SqlCommand.ExecuteNonQuery

喜夏-厌秋 提交于 2020-01-03 14:22:09
问题 In an approval workflow I want to ensure that reminder emails are sent exactly once. With SqlCommand.ExecuteNonQuery I can ensure this by testing the return value. What is the recommended solution using EF? According to the documentation ObjectContext.SaveChanges does not return an equivalent value. SqlCommand example: (The TransactionScope is used to rollback the DB update in case SendMail fails.) Dim sql = "UPDATE LeaveApprovalRequests SET State = 'Reminded'" & " WHERE ID=3 AND State <>

EF4.1 multiple nested entity Includes gets NotSupportedException?

房东的猫 提交于 2020-01-03 10:48:07
问题 Edit: Updated problem description based on testing - 12 Sep 2011. I have this query that throws a NotSupportedException ("Specified method is not supported.") whenever I call .ToList(). IQueryable<FileDefinition> query = db .FileDefinitions .Include(x => x.DefinitionChangeLogs) .Include(x => x.FieldDefinitions.Select(y => y.DefinitionChangeLogs)) // bad .Include(x => x.FieldDefinitions.Select(y => y.FieldValidationTables)) // bad .Where(x => x.IsActive); List<FileDefinition> retval = query

EF4.1 multiple nested entity Includes gets NotSupportedException?

此生再无相见时 提交于 2020-01-03 10:47:13
问题 Edit: Updated problem description based on testing - 12 Sep 2011. I have this query that throws a NotSupportedException ("Specified method is not supported.") whenever I call .ToList(). IQueryable<FileDefinition> query = db .FileDefinitions .Include(x => x.DefinitionChangeLogs) .Include(x => x.FieldDefinitions.Select(y => y.DefinitionChangeLogs)) // bad .Include(x => x.FieldDefinitions.Select(y => y.FieldValidationTables)) // bad .Where(x => x.IsActive); List<FileDefinition> retval = query