entity-framework-4.1

Showing SQL command with EF4.1's ToString()

﹥>﹥吖頭↗ 提交于 2019-12-24 09:08:16
问题 I have an EF4.1 database first application, and the .ToString() method is only giving me the reflected type, and not the SQL query. How do I make my application show me the Linq query in SQL format? I've added version 4.1 of C:\Program Files (x86)\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll to my project. I'm guessing the ToString feature is added by an extension method, but even when I add "Using system.data.Entity" I'm unable to getthe string output to work. Does

Entity Framework 4 Partialy SaveChanges

隐身守侯 提交于 2019-12-24 06:47:44
问题 Lets say i have this: var entity = db.histories.GetWhere(x => x.Body == "MyBody").FirstOrDefault(); var entity2 = db.histories.GetWhere(x => x.Body == "MyBody2").FirstOrDefault(); entity.From = "lmao!"; entity2.From = "lmao2!"; now i know that to update i have to call db.SaveChanges(); my question is what if i want to update entity only and not entity2 ? is that even possible ? could be simple im not sure. thanks in advance. 回答1: Either get the 2 entities from separate contexts: var entity =

Entity Framework - Join to a List

南笙酒味 提交于 2019-12-24 05:09:09
问题 I need to retrieve a list of entities from my database that matches a list of items in a plain list (not EF). Is this possible with Entity Framework 4.1? Example: var list = new List<string> { "abc", "def", "ghi" }; var items = from i in context.Items where list.Contains(i.Name) select i; This works great to return rows that match one property, but I actually have a more complex property: var list = new List<Tuple<string, string>> { new Tuple<string,string>("abc", "123"), new Tuple<string

Saving Entity causes duplicate insert into lookup data

烈酒焚心 提交于 2019-12-24 03:32:16
问题 I am using EF 4.1 "code first" to create my db and objects. Given: public class Order { public int Id { get; set; } public string Name { get; set; } public virtual OrderType OrderType { get; set; } } public class OrderType { public int Id { get; set; } public string Name { get; set; } } An order has one ordertype. An order type is just a look up table. The values dont change. Using Fluent API: //Order ToTable("order"); HasKey(key => key.Id); Property(item => item.Id).HasColumnName("order_id")

Controlling how often an Entity Framework ObjectContext is created

≡放荡痞女 提交于 2019-12-24 01:49:17
问题 I have a rather large SQL backed ASP.NET project that uses Entity Framework to interact with the database. My question is, should I create an instance of the ObjectContext defined by the edmx/designer files in each class/method -or- wrap it around a static class that would instantiate onload and basically handle all requests through one instance. I will have multiple users using the ObjectContext to read and update the DB, concurrency and thread safety are my utmost concerns. EDIT: This code

Why aren't my foreign key reference properties reflecting the foreign key ID property values?

早过忘川 提交于 2019-12-24 00:59:55
问题 I have a Candidate entity, with some associations exampled below. I have a view model that doesn't have the reference properties, just the foreign key properties, and I am using AutoMapper to map the view model back to an entity. When I try and save the entity, I get validation errors telling me that e.g. The Title field is required. because although my TitleId has a valid value, Title is still null. public class Candidate { ... [Required] public string RefNum { get; set; } [ForeignKey(

Generate POCO classes and the mapping for an existing database using Entity Framework

限于喜欢 提交于 2019-12-24 00:44:00
问题 Is it possible to auto generate the POCO classes and the mapping with the database defined separately using Fluent API (instead of annotations) for an existing database? Instead of coding all these entity classes manually, I find it easier if they are auto generated and then I can change them as required if the names are not incorrect (plural or singular) or the some of the relationships are not correctly mapped etc. This will save lot of time for me compared to coding all the entity classes

How to get MVC-mini-profiler working on EF 4.1 Database First

大城市里の小女人 提交于 2019-12-24 00:14:12
问题 Spent better part of the day combing through SO for this. Here's the setup MVC 3 App using Repository pattern with 3 repositories across 2 databases. EF 4.1 Database first using the DBContext API for both db connections. Installed mvc-mini-profiler from nuget This is how I create my Db Context in the repository public class TransactionRepository : BaseRepository, ITransactionRepository { AccountingEntities _db = new AccountingEntities(); // repository methods } Then in controllers public

Difficulty Concerning EF Code First Fluent API, TPH, and Foreign Keys

人走茶凉 提交于 2019-12-23 19:44:53
问题 I have two tables in my database. One is called Users , and the other is called Widgets . The Widgets table represents 3 entities in my code model. One of the entities, Widget , is a parent class for the other two entities, WidgetTypeA and WidgetTypeB . Both WidgetTypeA and WidgetTypeB have navigation properties to the User entity, which is persisted to the Users table in the database. I'm having trouble getting Code First to use the same foreign key for both the WidgetTypeA and WidgetTypeB

Getting “Cannot insert the value NULL into column” when trying to save with .Add() method using DbContext . Please check my POCO's and save method

蹲街弑〆低调 提交于 2019-12-23 19:12:06
问题 Used code first and everything appears to work apart from the below which also worked before when I used ObjectContext and called context.PCBuilds.AddObject(pcBuild) but after switching to DbContext it's giving me the error. EFDbContext context = new EFDbContext(); public ActionResult Index() { PCBuild pcBuild = new PCBuild(); pcBuild.BuildID = 34245; pcBuild.BuildName = "Test99"; pcBuild.MarkUp = 25; pcBuild.BDetails = new List<BDetail>(); context.PCBuilds.Add(pcBuild); //repository.PCBuilds