entity-framework-5

Entity Framework Code Migrations - Stuck on Initial Migration

自古美人都是妖i 提交于 2019-11-28 20:35:30
I have added EF 5 to my project via Nuget and enabled migrations with the "Enable-Migrations" command. I have then called "Add-Migration" to generate the base code for generating the schema. I then added a property to one of my domain objects (string property called "TestProperty") and added a mapping to my EntityTypeConfiguration file (we're ignoring the conventions at the moment). Calling "Add-Migration" again produces the error: Unable to generate an explicit migration because the following explicit migrations are pending: [201303262144218_Initial]. Apply the pending explicit migrations

Why does the Entity Framework generate nested SQL queries?

吃可爱长大的小学妹 提交于 2019-11-28 20:26:26
Why does the Entity Framework generate nested SQL queries? I have this code var db = new Context(); var result = db.Network.Where(x => x.ServerID == serverId) .OrderBy(x=> x.StartTime) .Take(limit); Which generates this! (Note the double select statement) SELECT `Project1`.`Id`, `Project1`.`ServerID`, `Project1`.`EventId`, `Project1`.`StartTime` FROM (SELECT `Extent1`.`Id`, `Extent1`.`ServerID`, `Extent1`.`EventId`, `Extent1`.`StartTime` FROM `Networkes` AS `Extent1` WHERE `Extent1`.`ServerID` = @p__linq__0) AS `Project1` ORDER BY `Project1`.`StartTime` DESC LIMIT 5 What should I change so

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First

与世无争的帅哥 提交于 2019-11-28 19:24:11
问题 I get this error: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. when i try to update the database with the Update-Database command in the Package Manager Console. How can I write the lines to the output window in visual studio? I tried: try { context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\

ASP.NET MVC 4, EF5, Unique property in model - best practice?

点点圈 提交于 2019-11-28 18:24:06
ASP.NET MVC 4, EF5, Code First , SQL Server 2012 Express What is best practice to enforce a unique value in a model? I have a places class that has a 'url' property that should be unique for every place. public class Place { [ScaffoldColumn(false)] public virtual int PlaceID { get; set; } [DisplayName("Date Added")] public virtual DateTime DateAdded { get; set; } [Required(ErrorMessage = "Place Name is required")] [StringLength(100)] public virtual string Name { get; set; } public virtual string URL { get; set; } }; Why isn't there just a [Unique] data annotation you can place on it? I have

Entity Framework HierarchyId Workarounds

亡梦爱人 提交于 2019-11-28 17:38:26
EF 5.0 I am working on a prototype to test hierarchyid and entity framework together. I have the following schema: Create Table dbo.Employee ( EmployeeId int identity not null, Name nvarchar(100) not null, Node hierarchyid not null, NodePath as Node.ToString() persisted, Level AS Node.GetLevel() persisted, ManagerNode as Node.GetAncestor(1) persisted, ManagerNodePath as Node.GetAncestor(1).ToString() persisted ); Alter Table dbo.Employee Add Constraint EmployeePK Primary Key NonClustered (EmployeeId); Go --Enforce Hierarchy Alter Table dbo.Employee Add Constraint EmployeeManagerNodeNodeFK

Entity framework 5.0 handle optimistic concurrency exception?

心不动则不痛 提交于 2019-11-28 17:18:32
When handling several potential exceptions during a context.SaveChanges() one of the exceptions is OptimisticConcurrency . Microsoft's documentation on this at http://msdn.microsoft.com/en-us/library/bb399228.aspx discusses this for EF 4.x ... try { // Try to save changes, which may cause a conflict. int num = context.SaveChanges(); Console.WriteLine("No conflicts. " + num.ToString() + " updates saved."); } catch (OptimisticConcurrencyException) { // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. context.Refresh(RefreshMode.ClientWins, orders); /

Adding CreatedDate to an entity using Entity Framework 5 Code First

放肆的年华 提交于 2019-11-28 17:14:50
I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UTC date. I do NOT want to use a constructor, as I have many entities in my model that I want to inherit from an abstract class containing the CreatedDate property, and I can't enforce a constructor with an interface. I have tried different data annotations and I have attempted to write a database initializer that would pick up a specific entity type and write an alter constraint with a getdate() default value for the correct table_name

How to set up LocalDb for unit tests in Visual Studio 2012 and Entity Framework 5

荒凉一梦 提交于 2019-11-28 16:58:02
问题 We have a Visual Studio 2012 ASP.NET MVC project using Entity Framework 5. There are some unit tests that depend on a database. Setting up the app.config file in the test project to use a central SQL Server database works fine. However, it would be much nicer to use a LocalDb, so that each developer has his/her own database when running the tests. Especially since we would like to have the tests set up to DropCreateDatabaseAlways when running. However, I can't get the setup to work. If I try

How to install EntityFramework 5.0 (and other older versions) from NuGet?

牧云@^-^@ 提交于 2019-11-28 16:50:52
I installed EF 5.0 into the .DAL, now I want to install EF 5.0 into .BLL from Nuget But the default online Nuget package always comes up with 6.0, which gives error when used together with the 5.0 EF I use in DAL. Where can I install the 5.0 version of EntityFramework into my Nuget packages?? View -> Other windows -> Package Manager Console then run install-package entityframework -version 5.0.0.0 . Add -project <project.name> if you want to install it in a specific project. here is what I experienced: (before I begin... kudos to devnull for the answer). I clicked on: 1) View -> Other windows

The term 'Update-Database' is not recognized as the name of a cmdlet

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 16:46:34
I am using EF5 beta1 and while I was able to run the "Update-Database" before. Now that I shut down Visual Studio, I cannot get it to run. I get the following error: The term 'Update-Database' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:16 + Update-Database <<<< -verbose + CategoryInfo : ObjectNotFound: (Update-Database:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException I have tried to re-install