Limit Number of Results being returned in a List from Linq

Use the Take function int numberOfrecords=10; // read from user listOfItems.OrderByDescending(x => x.CreatedDate).Take(numberOfrecords) Assuming listOfItems is List of your entity objects and CreatedDate is a field which has the date created value (used here to do the Order by descending to get recent items). Take() Function returns a specified number of contiguous elements from the … Read more

Stored Procedures and updating EDMX

(This solution is for EF 6. I have not tried in other EF versions. It works nice.) Go to Model browser. MyStoreProc is the name of the stored procedure (as an example). MyStoreProc will appear in 3 places. 1st place- Under Complex Types-> as MyStoreProc_result 2nd Place- Under Function Imports -> as MyStoreProc 3rd Place … Read more

Entity Framework is Too Slow. What are my options? [closed]

The fact of the matter is that products such as Entity Framework will ALWAYS be slow and inefficient, because they are executing lot more code. I also find it silly that people are suggesting that one should optimize LINQ queries, look at the SQL generated, use debuggers, pre-compile, take many extra steps, etc. i.e. waste … Read more

Default value for Required fields in Entity Framework migrations?

In addition to the answer from @webdeveloper and @Pushpendra, you need to manually add updates to your migration to update existing rows. For example: public override void Up() { Sql(“UPDATE [dbo].[Movies] SET Title=”No Title” WHERE Title IS NULL”); AlterColumn(“dbo.Movies”, “Title”, c => c.String(nullable: false,defaultValue:”MyTitle”)); } This is because AlterColumn produces DDL to set the default … Read more

Nullable property to entity field, Entity Framework through Code First

Just omit the [Required] attribute from the string somefield property. This will make it create a NULLable column in the db. To make int types allow NULLs in the database, they must be declared as nullable ints in the model: // an int can never be null, so it will be created as NOT NULL … Read more