entity-framework-4.3

Mapping a self-join to a collection in code first entity framework 4.3

你。 提交于 2019-12-10 17:22:34
问题 I have a POCO class that maps to a self-joined table defined like this: CREATE TABLE [dbo].[GalleryCategories]( [CategoryID] [int] IDENTITY(0,1) NOT NULL PRIMARY KEY CLUSTERED, [Name] [nvarchar](256) NOT NULL, [ParentID] [int] NULL REFERENCES [dbo].[GalleryCategories] ([CategoryID]) ON DELETE NO ACTION ON UPDATE NO ACTION, ) I know there's a way to define a relationship using the model builder to reference a parent from a child... (e.g. like this) But the class I'm trying to map looks like

Entity Framework Code First - Cannot insert duplicate key in object 'dbo.T_CRProviders'

天涯浪子 提交于 2019-12-10 16:43:29
问题 I have some urgent issue which I could not find answer for across the web. I am using CodeFirst EF 4.3.1 and I am getting an error: Violation of PRIMARY KEY constraint 'PK_T_CRProviders'. Cannot insert duplicate key in object 'dbo.T_CRProviders'. My code is: Models: public enum CRProviderEnums { PE_Abcd = 0, PE_Efgh } [Table("T_CRProviders")] public class CRProvider { [Key] [Required] public int Enum { get; set; } [Required] public string Name { get; set; } } [Table("T_CRSupportedResources")]

Pre-Generate Views for EF Code First

隐身守侯 提交于 2019-12-10 10:15:42
问题 Having finally deployed to live a new site built with Entity Framework Code First and MySql, one nagging annoyance is the pause often experienced while view metadata is cached. This seems to happen far more frequently than the application pool is recycled. (?) I guess that for a site which has a constant stream of traffic this might not be noticed often. For a new site with infrequent traffic it feels like every time I go to the site there's a 4-5 second delay in rendering the first page. So

Entity Framework 4.3.1 failing to create (/open) a database [Threading Anomaly?]

我只是一个虾纸丫 提交于 2019-12-10 03:54:31
问题 I've used EF 4.1 ( Code First ) in an MVC 3 project a while back, and it was good. Today I tried using EF 4.3.1 ( Code First ) in a WinForms project and encountered some real voodoo: (The original project I was working on was WinForms, however the same thing is true for the attached Console Application code.) When trying to enter a simple class into the database, I get the following exception: Cannot open database "Test" requested by the login. The login failed. Login failed for user '

Entity Framework 4.3 with MVC on Edit doesn't save complex object

落花浮王杯 提交于 2019-12-09 07:12:37
问题 I made a small project with Northwind database to illustrate the problematic. Here is the action of the controller : [HttpPost] public ActionResult Edit(Product productFromForm) { try { context.Products.Attach(productFromForm); var fromBD = context.Categories.Find(productFromForm.Category.CategoryID); productFromForm.Category = fromBD; context.Entry(productFromForm).State = EntityState.Modified; context.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } context is

Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

青春壹個敷衍的年華 提交于 2019-12-09 00:25:44
问题 I'm currently using EF Code First 4.3 with migrations enabled, but automatic migrations disabled. My question is simple, is there a data annotations equivalent of the model configuration .WillCascadeOnDelete(false) I would like to decorate my class so that the foreign key relationships do NOT trigger a cascading delete. Code sample: public class Container { public int ContainerID { get; set; } public string Name { get; set; } public virtual ICollection<Output> Outputs { get; set; } } public

Entity Framework CodeFirst Move Data From One table To another

折月煮酒 提交于 2019-12-08 12:32:49
问题 I'm using Entity Framework(4.3) Code First Method For My Asp.Net Mvc3 Application.I want to do:Data of table A must be copied (along with some other data) to table B after that when Click Save Button Tabla A Data will be Removed how to implement this? 回答1: Here are the logical steps to take. Add the following to the Save button's click event: Use a loop to iterate over each row in table A. While looping, add the row information from table A, along with the other data that must be copied, to

How can I do TDD and Unit Testing for EF Code First entity declaration and mapping?

亡梦爱人 提交于 2019-12-08 09:18:29
问题 I have am about to retro-code a suite of unit tests for a new MVC4 app. Because nearly all my code in the EF data project is copied straight from code generated by the VS2012 EF Reverse Engineering tool, I have decided to skip unit tests in this part of the application, unless I can somehow automatically generate them. I have no business logic at here and I would like to first concentrate my efforts on ensuring better QA on the business side. But, I would like to know how one goes about first

How can my DbContext contain the same entity twice?

允我心安 提交于 2019-12-08 07:40:58
问题 I had to do some gymnastics to update an entity that contained a reference to another entity and I do not understand why, because in my side test project it works without. The problem seems to be that the DbContext contains multiple entities with the same primary key but with other property changed. So, I have to do something like the code below to set the reference correctly. If I do not do this, I create a new "Tax" entity in the database, which is not what I want but a reference to the

How to add entities to DbContext with recursive relationships while avoiding duplicate additions?

。_饼干妹妹 提交于 2019-12-08 05:47:02
问题 I'm using Entity Framework 4.4 and I have a One-To-Many relationship model like this: class Item { public string keyPart1 { get; set; } public string keyPart2 { get; set; } public virtual Container container { get; set; } public string ContainerId { get; set; } } // Idea is that many Items will be assigned to a container class Container { public string ContainerId { get; set; } private ICollection<Item> _Items; public virtual ICollection<Item> As { get { return _Items ?? (_Items = new HashSet