entity-framework-4.1

How to select just some fields from a table in EF

人盡茶涼 提交于 2019-12-02 20:54:20
I have a table with 9 columns in database and I want to be able to load only some fields of it if I need. How can I do this with Entity Framework 4 please? e.g. My table has these fields: ID, FirstName, LastName, FotherName, BirthDate, Mobile, Email and I want to be able to fetch just these columns: ID, FirstName, LastName My project is an ASP.NET MVC 3 application, with SQLServer 2008 Express and EF 4.1 . Assume you have a table with this model: public class User{ public int ID {get; set;} public string NickName {get; set;} public string FirstName {get; set;} public string LastName {get; set;

Entity Framework Proxy creation

廉价感情. 提交于 2019-12-02 20:45:13
We can stop creation of proxy in the context constructor by using this.Configuration.ProxyCreationEnabled = false; What are the advantages and disadvantages of creating proxies in EF 4.1 ? Proxies are necessary for two features: Lazy loading - navigation properties are loaded once accessed first time Dynamic change tracking - if you modify any property in the entity the context is notified about this change and set the state of the entity. If dynamic change tracking is not used, context must use snapshot change tracking which means discovery all changes before saving takes place = exploring

Entity Framework Code-First: How to manually update the database?

佐手、 提交于 2019-12-02 20:27:56
I've build a little WPF demo app which uses EF Code-First to save its data in a SQL CE 4.0 DB. It works fine unless I remove a property from a model object. For example, if I remove "HosteBy" from this class..... public class Dinner { public int DinnerID { get; set; } public string Title { get; set; } public DateTime EventDate { get; set; } public string Address { get; set; } public string HostedBy { get; set; } public virtual ICollection<RSVP> RSVPs { get; set; } } ...it throws this exception: The model backing the 'NerdDinners' context has changed since the database was created. Either

Code First Mapping for Entity Framework Hierarchy

匆匆过客 提交于 2019-12-02 20:17:37
问题 I have a model that looks like this: public class Category { public string Id { get; set; } public string Description { get; set; } public Category Parent { get; set; } public ICollection<Category> Children { get; set; } public ICollection<Product> Products { get; set; } } With a database table that looks like Categories Id (PK varchar(5)) Description (nvarchar(50)) ParentId (FK varchar(5)) But Im stumped when it comes to setting up the mapping modelBuilder.Entity<Category>() .HasMany(x => x

MVC3 and EF Data first: what are the best practices?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 19:42:33
It seems that most of the focus with MVC3 and EF4.1 is around "code first" - I can't seem to find any examples or tutorials that meet the following criteria: uses an existing SQLServer database has separate projects for web & data access (we will have multiple web apps sharing the same data access classes) recommendations for validation Does such an example or tutorial exist? Are there any documented "best practices" for how to accomplish this, or rationale for NOT having a solution structured this way? Ladislav Mrnka It is quite common scenario and it depends if you want to use EDMX file for

Entity Framework Performance Issue

亡梦爱人 提交于 2019-12-02 19:22:14
I am running into an interesting performance issue with Entity Framework. I am using Code First. Here is the structure of my entities: A Book can have many Reviews. A Review is associated with a single Book. A Review can have one or many Comments. A Comment is associated with one Review. public class Book { public int BookId { get; set; } // ... public ICollection<Review> Reviews { get; set; } } public class Review { public int ReviewId { get; set; } public int BookId { get; set; } public Book Book { get; set; } public ICollection<Comment> Comments { get; set; } } public class Comment { public

EF 4.1 Code First and Existing Database and .NET Membership

浪子不回头ぞ 提交于 2019-12-02 19:20:30
I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box. I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message: The server encountered an error processing the request. The exception message is "Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions. After some googling, I discovered that I had to delete the database and

Entity Framework conditional mapping with code first?

十年热恋 提交于 2019-12-02 17:51:03
问题 I have an entity that I need to return only records where a given field value is greater than zero. I have seen examples of conditional mapping in the edmx and that seems like what I am in need of. However, my project is in EF 4.1 code first. Is there not a way to do this using the code first approach? 回答1: I dont think there is an inbuilt method for achieving this, you can however expose a property in your DbContext in which you apply filtering, initially this will be readonly but i dont see

Entity Framework 4.1 Code First Foreign Key Id's

断了今生、忘了曾经 提交于 2019-12-02 16:46:36
I have two entities referenced one to many. When entity framework created the table it creates two foreign keys, one for the key I have specified with the fluent interface and the other for the ICollection. How do I get rid of the duplicate foreign key? public class Person { public long RecordId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Username { get; set; } public long DepartmentId { get; set; } public virtual Department Department { get; set; } } public class Department { public long RecordId {

How to work with navigation properties (/foreign keys) in ASP.NET MVC 3 and EF 4.1 code first

梦想与她 提交于 2019-12-02 15:14:39
问题 I started testing a "workflow" with EF code first. First, I created class diagram. Designed few classes - you can see class diagram here Then I used EF Code First, created EntsContext.. public class EntsContext : DbContext { public DbSet<Project> Projects { get; set; } public DbSet<Phase> Phases { get; set; } public DbSet<Iteration> Iterations { get; set; } public DbSet<Task> Tasks { get; set; } public DbSet<Member> Members { get; set; } } Next step was creating a ProjectController (ASP.NET