entity-framework-4.1

error when using interfaces for Entity Framework (4.2) entities

 ̄綄美尐妖づ 提交于 2019-11-28 01:55:51
I am using the the latest version of Entity Framework (4.2) and trying to implement interfaces for my Entities and for some reason, it isn't compiling. it is throwing an error " Cannot convert expression type ICollection<IOrder> to return type ICollection<Order> ". if I don't use interfaces for the entities, then I don't get this error. I have a separate project for interfaces (for repositories and services etc) and I need to pass the EF entities in those methods as parameters and I don't want to pass the actual entities in them, because that will require the interface project to have a

EF code-first: How to load related data (parent-child-grandchild)?

佐手、 提交于 2019-11-28 00:39:08
问题 I have this entity: public class DynamicPage { public int PageId { get; set; } public int Order { get; set; } public string MenuText { get; set; } public string MenuHover { get; set; } public int? ParentId { get; set; } public virtual DynamicPage Parent { get; set; } public virtual ICollection<DynamicPage> Children { get; set; } } This entity may have 3 level: Parent -> Child -> Grandchild. How can I load the Parent (level 1) whit all associated children (level 2) and for each child,

Store read-only calculated field with Entity Framework Code First

Deadly 提交于 2019-11-28 00:08:10
问题 I am using Entity Framework Code First, and I have an entity defined with a StartTime property, an EndTime property and a Duration property (along with some others). The Duration property is a calculated field and it is the duration in minutes between the start and end time. My property declarations are shown below: public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public int Duration { get { return (int)this.EndTime.Subtract(this.StartTime).TotalMinutes; } } I

EF 4.1 messing things up. Has FK naming strategy changed?

流过昼夜 提交于 2019-11-28 00:06:03
问题 I've just installed the new Entity Framework 4.1 NuGet package, thus replacing the EFCodeFirst package as per NuGet intructions and this article of Scott Hanselman. Now, imagine the following model: public class User { [Key] public string UserName { get; set; } // whatever } public class UserThing { public int ID { get; set; } public virtual User User { get; set; } // whatever } The last EFCodeFirst release generated a foreign key in the UserThing table called UserUserName . After installing

DbSet table name

Deadly 提交于 2019-11-27 23:34:43
To get database table name on Entity framework 4.0 I do: ObjectSetInstance.EntitySet.ToString() Is there a way to do this on Entity Framework 4.1? Try something like this: string name = (context as IObjectContextAdapter).ObjectContext.CreateObjectSet<MyClass>().EntitySet.Name; Extension methods for DbContext and ObjectContext : public static class ContextExtensions { public static string GetTableName<T>(this DbContext context) where T : class { ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext; return objectContext.GetTableName<T>(); } public static string

Error storing Image in SQL CE 4.0 with ASP.NET MVC 3 and Entity Framework 4.1 Code First

这一生的挚爱 提交于 2019-11-27 23:29:57
问题 I'm trying to store/save an image in an SQL Compact Edition (CE) database. I declare the field in my Student model as: [Column(TypeName = "image")] public byte[] Photo { get; set; } The database is created with the image data type for the Photo column as can be seen here: The problem is: When I run the app and try to save a Student with a Photo of 3 MB (for example), I get an exception: validationError.ErrorMessage = "The field Photo must be a string or array type with a maximum length of

How to retrieve last 5 records using LINQ method or query expression in C#

心已入冬 提交于 2019-11-27 23:24:25
问题 On my homepage, I want to show the recently added products. I have added a ChildAction to my controller but i am unable to understand what Linq query should i run to fetch the last five records. 回答1: LINQ var lastFiveProducts = (from p in products orderby p.ProductDate descending select p).Take(5); Lambda var lastFiveProducts = products.OrderByDescending(p => p.ProductDate).Take(5); Which ever you prefer. 回答2: .Skip(count - 5); or .Reverse().Take(5).Reverse() 回答3: The simplest approach is to

How do I code an optional one-to-one relationship in EF 4.1 code first with lazy loading and the same primary key on both tables?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 22:34:21
问题 I'm working with an application and data structure built upon ASP/ADO.NET and I'm converting part of it to ASP.NET MVC. In the data structure, there exists a "optional one-to-one" relationship, where both tables use the same primary key, and name. Basically this table can be considered an "optional extension" of the primary table. Here are samples of the model: public class ZoneMedia { public int ZoneMediaID { get; set; } public string MediaName { get; set; } public int Width { get; set; }

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

允我心安 提交于 2019-11-27 22:34:18
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 has a reference to 'EntityFramework 4.3.1'. You're using ObjectContext , which does not have the Entry

DbContext won't keep connection open for re-use

随声附和 提交于 2019-11-27 22:11:43
问题 I'm trying to reuse an existing database connection so that I can do multiple database operations using a TransactionScope without invoking MSDTC. Entity Framework (using the new DbContext API in the 4.1 release) doesn't seem to want to keep an explicitly-opened connection open. The old ObjectContext API keeps the connection open as expected and documented. Since the DbContext API just uses ObjectContext under the hood, I'd have expected the same behaviour. Does anyone know if this change is