entity-framework-4.1

How to pass multiple Expressions to OrderBy for EF?

倖福魔咒の 提交于 2019-11-29 08:43:25
问题 I am using EF 4.2, but I expect this would apply to EF 4 and 4.1 as well. I would like to pass an IQueryable<T> and multiple Expression<Func<TSource, TKey>> to a method and have the method apply OrderBy and ThenBy to the IQueryable<T> as appropriate. I found this answer, and wrote the method below based on that: public IQueryable<User> ApplyOrderBy(IQueryable<User> query, IEnumerable<Expression<Func<User, IComparable>>> orderBy) { if (orderBy == null) { return query; } IOrderedQueryable<User>

Class Inheritance with .NET EF4.1 + MySQL

醉酒当歌 提交于 2019-11-29 08:10:05
I'm trying to implement class inheritance in .NET using Entity Framework 4.1 and MySQL as database, with code-first approach. The model below works with SQL Server, but fails in MySQL with the following error: Schema specified is not valid. Errors: (11,6) : error 0064: Facet 'MaxLength' must not be specified for type 'mediumtext'. The model is a classic simple example: public abstract class Vehicle { public int Id { get; set; } public int Year { get; set; } } public class Car : Vehicle { public string CarProperty { get; set; } } public class Bike : Vehicle { public string BikeProperty { get;

Update a single property of a record in Entity Framework Code First

喜夏-厌秋 提交于 2019-11-29 07:21:27
问题 How can I update a single property of a record without retrieving it first? I'm asking in the context of EF Code First 4.1 Says I have a class User, mapping to table Users in Database: class User { public int Id {get;set;} [Required] public string Name {get;set;} public DateTime LastActivity {get;set;} ... } Now I want to update LastActivity of a user. I have user id. I can easily do so by querying the user record, set new value to LastActivity, then call SaveChanges(). But this would result

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

拥有回忆 提交于 2019-11-29 07:05:39
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, associated grandchild (level 3) if any? Thanks to help. EF 4.1 feature and syntax: var entity = context.Parents

Store read-only calculated field with Entity Framework Code First

柔情痞子 提交于 2019-11-29 06:21:04
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 would like to run this calculation in code, but have the duration value persisted to the database along

Can I specify a discriminator column with a table-per-type mapping?

一世执手 提交于 2019-11-29 05:38:50
I have a class hierarchy that I want to map across several tables using Entity Framework 4.1 Code First. It's like table-per-type (TPT) but I also want a discrimator column. The hierarchy looks something like: public class Event { public Guid Id { get; set; } public string Code { get; set; } // discriminator public DateTime Date { get; set; } } public class Party : Event { public int AttendeeCount { get; set; } } public class BirthdayParty : Party { public int Age { get; set; } } public class WeddingParty : Party { public string Surname { get; set; } } This is a pretty weak example but I hope

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

安稳与你 提交于 2019-11-29 05:37:21
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 '4000'." SQL Server CE supports these Data Types . In this comparison between SQL Express and SQL Compact

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

不羁岁月 提交于 2019-11-29 05:34:54
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. 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. .Skip(count - 5); or .Reverse().Take(5).Reverse() The simplest approach is to reverse your ordering (e.g. use orderby foo descending ) and then use Take() . For example: var recentProducts =

Entity Framework | Code First | Mapping of sub property from CultureInfo.Name

我只是一个虾纸丫 提交于 2019-11-29 05:17:25
I've got an entity like this : public class Course { public CultureInfo Culture {get; set;} : } And I want to map just the Name property of CultureInfo to a single column in the table that Entity Framework generates for me. How would I go about this? Currently I've tried looking at the OnModelCreating() method of the DbContext, but I'm not finding anything that is standing out. I'm using Entity Framework 4.1 in and MVC 3 application with a Code First approach. Many thanks. Unfrotunatelly EF can't do it this way. The workaround is defining separate property with the Name which will be mapped 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-29 05:09:09
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; } public int Height { get; set; } public virtual ZoneMediaText MediaText { get; set; } } public class