entity-framework-5

Entity Framework Multiple Column as Primary Key by Fluent Api

喜你入骨 提交于 2019-11-29 09:02:34
These are my simplified domain classes. public class ProductCategory { public int ProductId { get; set; } public int CategoryId { get; set; } public virtual Product Product { get; set; } public virtual Category Category { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } } public class Category { public int Id { get; set; } public string Name { get; set; } public int? ParentCategoryId { get; set;} } This is my mapping class. But it doesnt work. public class ProductCategoryMap : EntityTypeConfiguration<ProductCategory> { public ProductCategoryMap(

How to Update only single field using EF

霸气de小男生 提交于 2019-11-29 08:50:37
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 all the entries , what i want is to just update the "Date" field , whose value i have got in

Many-to-many relationship left and right keys flipped after Entity Framework 5 upgrade

﹥>﹥吖頭↗ 提交于 2019-11-29 07:14:50
问题 I have some code that saves a many to many relationship in code. It was working fine with Entity Framework 4.1 but after updating to Entity Framework 5, it's failing. I'm getting the following error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_WebUserFavouriteEvent_Event". The conflict occurred in database "MainEvents", table "dbo.Event", column 'Id'. I'm using POCO entities with custom mappings. Standard field and many-to-one relationship mappings seem to be working

EF code-first: How to load related data (parent-child-grandchild)?

拥有回忆 提交于 2019-11-29 07:05:39
I have this entity: public class DynamicPage { public int PageId { get; set; } public int Order { get; set; } public string MenuText { get; set; } public string MenuHover { get; set; } public int? ParentId { get; set; } public virtual DynamicPage Parent { get; set; } public virtual ICollection<DynamicPage> Children { get; set; } } This entity may have 3 level: Parent -> Child -> Grandchild. How can I load the Parent (level 1) whit all associated children (level 2) and for each child, associated grandchild (level 3) if any? Thanks to help. EF 4.1 feature and syntax: var entity = context.Parents

Unable to run EF5 migration with existing database

我的未来我决定 提交于 2019-11-29 06:56:07
First, I've read these questions/answers: EF-migration message How can I stop Add-Migration checking my database has no pending migrations when using Code-Based migrations? Make EF4.3 Code First Migrations ignore pending migrations These all seem to be for EF versions prior to EF5, and my situation doesn't seem to fit these answers. So, let me describe my situation. My application was originally created using EF4, model first. I designed my database with the GUI designer, and used it to generate my database. I've been running and collecting data into the database for a few months. I really can

Changing column default values in EF5 Code First

若如初见. 提交于 2019-11-29 06:15:27
I'm trying to use CF to build a model for an existing database. I have a column in which I forgot to set a sane default value. And rather than compromise the purity of the initial migration by changing it, I just figured I'd create another migration (that's what migrations are for, right? :) public override void Up() { AlterColumn("Config", "DefaultTaxPerDollar", c => c.Decimal(nullable: false, precision: 19, scale: 5, defaultValue: 0.087m)); } public override void Down() { AlterColumn("Config", "DefaultTaxPerDollar", c => c.Decimal(nullable: false, precision: 19, scale: 5, defaultValue: 0.0m)

EF 4.3.1 and EF 5.0 DbSet.Local is slower than an actual Database query

佐手、 提交于 2019-11-29 05:59:36
问题 I've got a database with a table of about 16,500 cities, and an EF Data Model (Database-First) for that database. I preload them into memory with the code: Db.Cities.Load() ...then when it's time to use them, I've tried each of the following queries: Dim cities() As String = Db.Cities.Select(Function(c) c.CityName).ToArray Dim cities() As String = Db.Cities.Local.Select(Function(c) c.CityName).ToArray The first query is fast (~10ms), but the second one takes about 2.3 seconds to run the first

X Already contains a definition Y with EntityFramework? (simple database)

徘徊边缘 提交于 2019-11-29 03:42:46
I have 3 tables in my MS SQL database and I have added a EntityFramework(latest) to my project where I have imported these 3 tables. The first problem was that no Entities was built so I changed "Code Genereation Strategy" from None to Default . After build I get X Already Contains a definition for Y on all properties of the entities. When looking closer it have generated a partial ex Users.cs and one partial User in in the MainModel.Designer.cs ? Why is it generating User.cs ? I have a similar setup in another project and the EF is set with the same settings, there is no User.cs ? Edit1 : I

Cannot enable migrations for Entity Framework in class library

随声附和 提交于 2019-11-29 02:48:15
I just got on board with EF 5 and am using their code-first migrations tool but I seem to get an error when I try to enable migrations. I type Enable-Migrations into the package manager console and then it says No classes deriving from DbContext found in the current project. Edit the generated Configuration class to specify the context to enable migrations for. Code First Migrations enabled for project MyApp.MvcUI. It then creates a Migrations folder and a Configuration class in my MvcUI project. Thing is, my DbContext lives in a class library project called MyApp.Domain. It should be doing

EntityFramework 5 use dll version 4.4.0.instead 5.0

喜你入骨 提交于 2019-11-29 02:06:43
问题 I have project based on .NET 4.0 but I need to use EntityFramework 5 in my solution. So I install it from the NuGet. After that I change target framework to .NET 4.5 (I'm using VS 2012) and rebuild project. But in reference folder I see that project use EntityFramework.dll version 4.4.0.0. Ho can I use newer version of EntityFramework? in web.config <compilation debug="true" targetFramework="4.5"> <assemblies> <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken