entity-framework-4.1

Entity Framework 4.1 Retrieving self referencing data

亡梦爱人 提交于 2019-12-09 12:01:03
问题 I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I am struggling to get my self referencing setup properly. I have a Category class. It must be self referencing to itself. A category can be a parent category when the ParentCategoryId in the table is null. If a category has a ParentCategoryId with a value then it means that it belongs to a parent category. I followed this article on Code Project. Here is my Category class: public class Category { public int Id { get; set; }

Entity Framework Code First Mapping Foreign Key Using Fluent API

醉酒当歌 提交于 2019-12-09 10:00:06
问题 I have the situation where a User can have several addresses. Accordingly, I have an ICollection on my user class. But I also want the user to be able to choose a default address. So I've done the following: public class User { public int Id { get; set; } public int? DefaultAddressId { get; set; } [ForeignKey("DefaultAddressId")] public virtual Address DefaultAddress { get; set; } public virtual ICollection<Address> Addresses { get; set; } //properties were removed for purpose of this post }

EF 4.1 RC Many to many relationship in EF CF

故事扮演 提交于 2019-12-09 06:34:59
问题 I have two entities with many to many relationship like this: class author { public int AuthorID{get;set;} public string Name{get;set;} public virtual ICollection<book> books{get;set;} } class book { public int BookID{get;set;} public string Name{get;set;} public virtual ICollection<author> authors{get;set;} } and I have an intermediate table named Bookauthor defined like this: BookAuthor: int int AuthorID int BookID How to map this using FluentAPI Thanks!! 回答1: This was problematic with EDMX

EF 4.1 Mapping Problem

和自甴很熟 提交于 2019-12-09 05:52:53
问题 I have a class that have a relationship with itself: public class Person { public long ID { get; set; } public string Name { get; set; } public virtual Person Mother { get; set; } public virtual Person Father { get; set; } } When EF 4.1 is trying to map this class I'm getting the follow error: 'Unable to determine the principal end of an association between the types 'Model.Person' and 'Model.person'. The principal end of this association must be explicitly configured using either the

Mapping TPT in EF Code First 4.1 w/ Different Primary Keys

北城以北 提交于 2019-12-08 19:43:58
问题 I am trying to map a TPT inheritance hierarchy on a legacy database (I can't change column names). All of the examples have the primary keys of the parent and children tables with the same name. Unfortunately, mine doesn't behave this way. As a simplified example: Vehicle ---------------- VehicleId Make Model ---------------- Car ---------------- CarId SomeOtherField ---------------- CarId and VehicleId are actually the same id and are the values that should be used to associate the tables.

Does AsQueryable() on ICollection really makes lazy execution?

本小妞迷上赌 提交于 2019-12-08 19:21:07
问题 I am using Entity Framework CodeFirst where I have used Parent Child relations using ICollection as public class Person { public string UserName { get;set} public ICollection<Blog> Blogs { get; set;} } public class Blog { public int id { get; set; } public string Subject { get; set; } public string Body { get; set; } } Ok, so far everything is working ok, but my concern is, whenever I want to get the Blogs of a person, I get it as var thePerson = _context.Persons.Where(x => x.UserName = 'xxx'

Saving changes/updating existing object in dataset with Entity FrameWork and not have to set each property individually

吃可爱长大的小学妹 提交于 2019-12-08 19:21:02
问题 Can I do something like the below (which does not work) without having to explicitly set each property of the object. Product is the object that is created by the default model binder from a form submission and ProductInDb is object in the context/database that I wish to override/update. The ProductID primary key is the same on both. var ProductInDb = context.Products.FirstOrDefault(x => x.ProductID == product.ProductID); ProductInDb = product; context.SaveChanges(); 回答1: You can attach the

How can I define a stored procedure in Entity Framework (code first)?

可紊 提交于 2019-12-08 19:08:08
问题 I define my model in Entity Framework (code first), but how can I define a stored procedure in my model? I want to create my model and have it generate the stored procedure in my database when my database is generated from my model. 回答1: Description There is no built in direct mapping support for stored procedures in Entity Framework code first at the moment. But you can exec any sql query (create your stored procedure) like in my sample. Sample public class MyDatabaseContext : DbContext {

asp.net mvc 2 to mvc 4

时光怂恿深爱的人放手 提交于 2019-12-08 18:21:43
问题 I would like to update my project from: ASP.NET MVC 2 with Entity Framework 4. to ASP.NET MVC 4 and Entity Framework 4.1 How can this be done? 回答1: You may take a look at the ASP.NET MVC 4.0 release notes which explain how to upgrade from an ASP.NET MVC 3 application. And the release notes of ASP.NET MVC 3.0 explain how to upgrade from ASP.NET MVC 2. As far as upgrading from EF 4.0 to EF 4.1, it's as simple as updating the assembly reference. Of course if you wanted to do Code First approach

Getting Entities whose keys match list(or array) of ids

喜欢而已 提交于 2019-12-08 15:53:25
问题 I am using CodeFirst EntityFramework. I have a IQueryable<User> Entities that are returned using context.Users; where context is DbContext of EntityFramework. From this list I have to choose those whose Id is contained in an array of Ids (long). Id is primary key of User entity. I have tried the following but getting compiler error. IQueryable<User> users = GetQueryableUsers(); long [] ids = GetSelectedIds(); //array of long representing Ids key of User entities users.Intersect(ids); //