entity-framework-4

Entity Framework: Update related entities

陌路散爱 提交于 2019-12-23 19:57:43
问题 I have two entities: Invoice and InvoiceDetail. Invoice has an InvoiceDetails member. When I create an obcjet it works as expected. The framework inserts the Invoice and the InvoiceDetail rows in the database. $.ajax({ url: "/Invoices/Index", data: JSON.stringify({ InvoiceDetails: [{ Description: "1" }, { Description: "2" }] }), contentType: "application/json", type: "POST" }); [ActionName("Index")] [HttpPost] public JsonResult Post(Invoice invoice) { db.Invoices.AddObject(invoice); db

EntityDataSource replace * with % wildcard on queries

让人想犯罪 __ 提交于 2019-12-23 19:27:14
问题 I have an application that uses EntityDataSource in many places. In the EDS, I manually build the Where clause based on user input from TextBox'es. I would like the user to be able to enter "*" (asterisks) instead of "%" when querying data. Is there an easy as using Entity SQL or the EDS itself to do a search/replace? I know I could actually change the TextBox after the data is entered, but when the user sees his text was changed from an * to a % I don't think he will understand. I have tried

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

LINQ to Entities does not recognize method

孤街醉人 提交于 2019-12-23 18:16:42
问题 I have a long Linq To Entities query:- reports = db.CallProfileDailies .Join(db.ReportDailyTotals, cpd => cpd.Date, rdt => rdt.Date, (cpd, rdt) => new { cpd = cpd, rdt = rdt }) .Where(a => a.cpd.Skill == a.rdt.Skill) .Join(db.SummaryIntervalTotals, a => a.rdt.Date, sit => sit.Date, (a, sit) => new { cpd = a.cpd, rdt = a.rdt, sit = sit }) .Where(a => a.rdt.Skill == a.sit.Skill) .Select((a, i) => new ReportModel { AverageAbandonDelay = a.sit.AvgAbanTime, AverageAfterCallWorkTime = a.sit

Entity Framework: Map Foreign Key without Navigation Property?

北城余情 提交于 2019-12-23 17:46:54
问题 Motivation : My EF4.1 DbContext is saving Entities in the wrong order Reason : Lack of navigation properties on my models How I want to fix it : I want to set up foreign key relationships in my DbContext. The catch is that my entity objects have no navigation properties (I'm using it to populate a web service and then firing DTO objects over to my application). The classes below would be an example. In MinorClass, I want to configure my context so that it knows MajorClassID is a foreign key.

In EF4 Code first Collection are not lazy loading?

为君一笑 提交于 2019-12-23 15:14:08
问题 Using Microsoft Visual C# 2010 Express, Entity Framework Feature CTP4. I tried EF4 with code first with something small based on Scott Gu's blog. But it seems that collections are not initialized when retrieving an entity. I get a null reference exception when adding a product to a category. In all the examples I've seen, the collection are never explicitly initialized. What am I missing? Here's my code: using System; using System.Collections.Generic; using System.Linq; using System.Text;

How to mock LINQ to Entities helpers such as 'SqlFunctions.StringConvert()'

烂漫一生 提交于 2019-12-23 15:08:42
问题 I am using EF 4 and is trying to unit test the follow line using Moq: var convertError = models .Where(x => SqlFunctions.StringConvert((decimal?) (x.convert ?? 0)) == "0") .Any(); and it seems like SqlFunctions.StringConvert() will throw if it detects the context is mocked. It gives an error saying: This function can only be invoked from LINQ to Entities Is it possible to tell SqlFunctions.StringConvert to return a mock object so I can get rid of this error? 回答1: No it is not possible because

System.InvalidOperationException when trying to iteratively add objects using EF 4

自闭症网瘾萝莉.ら 提交于 2019-12-23 14:23:22
问题 This question is very similiar to this one. However, the resolution to that question: Does not seem to apply, or Are somewhat suspect, and don't seem like a good approach to resolving the problem. Basically, I'm iterating over a generic list of objects, and inserting them. Using MVC 2, EF 4 with the default code generation. foreach(Requirement r in requirements) { var car = new CustomerAgreementRequirement(); car.CustomerAgreementId = viewModel.Agreement.CustomerAgreementId; car.RequirementId

should I initialize collections in the constructor

冷暖自知 提交于 2019-12-23 13:08:11
问题 I have a many-to-many relationship between Foo and Bar Here's a simplified Foo public class Foo { public virtual ICollection<Bar> Bars {get;set;} } and I want to save a new foo with some bars attached: var foo = new Foo(); foo.Bars.Add(bar1); //I'll get an error that if Bars is not initialized repo.Insert(foo); repo.SaveChanges(); should I initialize the Bars in the constructor, is this how it should be done ? (using EF4 CTP5 codefirst) 回答1: I'd indeed initialize the collection in the

EF Code first : Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF

坚强是说给别人听的谎言 提交于 2019-12-23 12:47:32
问题 Set the primary key to identity in database (SQL Server) When using EF to insert data I am getting error Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF with this particular entity, though I am using the same approach with other entities to insert the data and getting no error with them. public class MyTable { [Key/*,DatabaseGenerated(DatabaseGeneratedOption.Identity)*/] public int ID{get;set;} public string name {get;set;} public string