entity-framework-4.1

EF 4.1 RC Many to many relationship in EF CF

a 夏天 提交于 2019-12-03 09:07:33
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!! This was problematic with EDMX but with EF 4.1 fluent API you can map it : modelBuilder.Entity<book>() .HasMany(b => b.authors) .WithMany

Entity Framework Code First Mapping

半世苍凉 提交于 2019-12-03 08:30:33
I have two classes, the Group class has a many to many relationship with the User class (representing the groups a user belongs to) and then the group also has a relationship of one to many with the user class (representing the owner of a group). How can I map this? public class User { public int Id { get; set; } public string Avatar { get; set; } public string Name { get; set; } public string Message { get; set; } public virtual ICollection<Group> OwnedGroups { get; set; } public virtual ICollection<Group> Groups { get; set; } } public class Group { public int Id { get; set; } public DateTime

entity framework code first - Union of the two fields into one collection

血红的双手。 提交于 2019-12-03 07:55:46
i have this model and configuration public class Person { public int? FatherId { get; set; } public virtual Person Father { get; set; } public int? MotherId { get; set; } public virtual Person Mother { get; set; } public virtual List<Person> Childs { get; set; } } class PersonConfiguration : EntityTypeConfiguration<Person> { public PersonConfiguration() { HasOptional(e => e.Father).WithMany(e => e.Childs) .HasForeignKey(e => e.FatherId); HasOptional(e => e.Mother).WithMany(e => e.Childs) .HasForeignKey(e => e.MotherId); } } and i get this error where the type is initial. Schema specified is

How to select just some fields from a table in EF

烂漫一生 提交于 2019-12-03 07:24:22
问题 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 . 回答1: Assume you have a table with this model: public class User{ public int ID {get;

EF 4.1 Mapping Problem

谁说我不能喝 提交于 2019-12-03 07:16:19
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 relationship fluent API or data annotations.' I already tried the solution of the topic EF 4.1 - Model

EF 4.1 code first causes weird (login) runtime errors

蓝咒 提交于 2019-12-03 07:14:23
I'm using EF 4.1 code first. Running into a very weird situation: Database does not exist, code is executed and as soon as the code wants to execute a query against the repository (using also repository pattern) MyRepsitory.Get(whereClause) i'm getting this error: Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'sa'. OK! So i tried to find what it is caused by and did the following: set a break point at the line which caused the error and as soon as this line was hit, i issued from the debugging (watch) window another query against the

Why does Entity Framework Code-First (with an existing DB) keep trying get data from an EdmMetadata table?

与世无争的帅哥 提交于 2019-12-03 07:12:40
i'm trying do some Entity Framework Code First programming to an existing database .. but I keep seeing this code in my Sql Profiler :- SELECT TOP ( 1 ) [Extent1].[Id] AS [Id], [Extent1].[ModelHash] AS [ModelHash] FROM [dbo].[EdmMetadata] AS [Extent1] ORDER BY [Extent1].[Id] DESC What the hell is this EdmMetadata table and why do is my EF code trying to grab the Id and ModelHash from there? Remember, I'm trying to work against an existing DB. Cheers :) Ladislav Mrnka There is no Code-First against existing database. If you have database you are doing Database-first. In such case your mapping

Entity Framework Proxy creation

你。 提交于 2019-12-03 07:08:30
问题 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 ? 回答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

Entity Framework: There is already an open DataReader associated with this Command which must be closed first

眉间皱痕 提交于 2019-12-03 07:03:43
This question is related to this : My repository method has this code: public IEnumerable<ApplicationPositionHistory> GetApplicationPositionHistories(int applicantId, int positionId) { return context.ApplicationsPositionHistory.Where(d => d.applicantPosition.ApplicantID == applicantId && d.applicantPosition.PositionID == positionId).Include(o => o.applicantPosition) ; } My Html has this code: @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.applicantPosition.Applicant.name) </td> <td> @Html.DisplayFor(modelItem => item.applicantPosition.Position.name) </td> The full

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

狂风中的少年 提交于 2019-12-03 06:48:14
问题 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