entity-framework-5

Getting 'Context is not constructible. Add a default constructor or provide an implementation of IDbContextFactory."

末鹿安然 提交于 2019-12-18 07:34:20
问题 I am getting this error when I try to use code first migrations. My context has a constructor with the connection name. public class VeraContext : DbContext, IDbContext { public VeraContext(string NameOrConnectionStringName = "VeraDB") : base(NameOrConnectionStringName) { } public IDbSet<User> Users { get; set; } public IDbSet<Product> Products { get; set; } public IDbSet<IntCat> IntCats { get; set; } } This connection name is injected with ninject when the project runs, I have also specified

How to Update only single field using EF

。_饼干妹妹 提交于 2019-12-18 05:24:09
问题 This is the current basic code : [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Registration registration) { if (ModelState.IsValid) { db.Entry(registration).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(registration); } I have around 15 fields in Registration table , how ever i just want to update the "Date" field , the object that i receive here is "registration" which only has value for a date , but the current code updates

Entity Framework partial load

怎甘沉沦 提交于 2019-12-18 03:48:11
问题 I have the following columns in my table Id (int) Name (nvarchar) usually < 100 characters Data (nvarchar) average 1MB I'm writing a program that will go through each row and perform some operations to the Name field. Since I'm only using the Name field and the Data field is very large, is it possible to direct EF to only load the Id and Name field? 回答1: Sure is ctx.YourDbSet.Select(p=> new { Id = p.Id, Name = p.Name}); this method is selecting into an anonymous class. if you want to save

Entity Framework 5 on .NET 4.0 - DatabaseGeneratedOption.Identity is undefined

风格不统一 提交于 2019-12-18 03:20:30
问题 I need to use EF5 on .NET 4 and I've run into a reference issue when mapping my class with HasDatabaseGenerationOption.Identity which doesn't exist in the 4.0 version of the library. The following is failing: this.Property(t => t.DeploymentLogId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); Does anyone know of a work around? 回答1: Using NuGet to add EntityFramework to a project that targets .NET 4.5, will add EntityFramework 5.0. If you later change the project to target .NET

Entity Framework 5 on .NET 4.0 - DatabaseGeneratedOption.Identity is undefined

浪尽此生 提交于 2019-12-18 03:20:05
问题 I need to use EF5 on .NET 4 and I've run into a reference issue when mapping my class with HasDatabaseGenerationOption.Identity which doesn't exist in the 4.0 version of the library. The following is failing: this.Property(t => t.DeploymentLogId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); Does anyone know of a work around? 回答1: Using NuGet to add EntityFramework to a project that targets .NET 4.5, will add EntityFramework 5.0. If you later change the project to target .NET

The object cannot be deleted because it was not found in the ObjectStateManager in entity framework 5

浪子不回头ぞ 提交于 2019-12-18 02:27:07
问题 I'm trying to delete an object using EntityFramework 5 but i get this error. The object cannot be deleted because it was not found in the ObjectStateManager I am using the Remove() method as DeleteObject() is not present in EF5. Can anyone help what am I missing? This does not work for Remove localDb.Customers.Remove(new Customer() { CustomerId = id }); localDb.SaveChanges(); Another thing I tried from msdn to change the state to Deleted. But here it gives an error saying all the fields

Why does the Entity Framework generate nested SQL queries?

主宰稳场 提交于 2019-12-17 23:13:42
问题 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

Entity Framework HierarchyId Workarounds

一个人想着一个人 提交于 2019-12-17 22:14:40
问题 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

Why my super simple ASP.NET Web API (mvc4)+Entity Framework 5 doesn't work?

拥有回忆 提交于 2019-12-17 20:31:27
问题 I spent days to know the problems of my work, but no luck. I created new MVC4 Web API project. Add EF5 with my database (Project>Add>ADO.NET Entity Data Model>Create from database which is in Azure SQL). Add two tables to edmx as below. And two *.tt files generate entities and model classes successfully. I can see the breakpoint(result) gives query result normally. But json gives abnormal stream without error message. (ie, http://localhost:41813/api/sheet/157 returns "157" which cannot

In what scenarios do I need foreign keys AND navigation properties in entity framework

不问归期 提交于 2019-12-17 19:16:24
问题 My Order class has: public int CustomerId { get; set; } public Customer Customer { get; set; } Do I really need both properties to make a relation working? I am not using disconnected entities, I am using code first approach. 回答1: According to Julia Lerman's book: Programming Entity Framework: DbContext, the difference lies at the difficulty of updating the navigation property. In page 85, She suggests "If there is one thing you can do to make your life easier in N-Tier scenarios, it’s to