entity-framework-5

How DbContext initializes automatic DbSet<T> properties?

早过忘川 提交于 2019-12-01 21:29:34
Consider the following class: class MyContext : DbContext { public DbSet<Order> Orders { get; set; } } and instantiating a new object: var mycontext = new MyContext(); Why mycontext.Orders is not null? When it was initialized? Who has initialized it? I'm really confused because the base class (DbConetxt) cannot access the derived class properties so it is not possible that the automatic property was initialized in the base object. From looking at the reflected code, when the DbContext (the base class) is constructed it makes a call to the DbSetDiscoveryService (an internal clasS) - which

Entity Framework 5 and Amazon RDS - “The underlying provider failed on Open.”

大城市里の小女人 提交于 2019-12-01 21:16:51
I have a C# / Entity Framework web application runs fine against a local SQL 2012 db. I copied the db out to a new RDS instance, and can access the db via Visual Studio and SQL Server Management Studio. I hav a unit test which authenticates the user and then attempts inserts a record in a table using an Entity Framework call -- dataContext.SaveChanges(). I get the following error: {"The underlying provider failed on Open."} {"No such host is known"} {"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not

How should I model friendships between users with EF code first?

老子叫甜甜 提交于 2019-12-01 20:29:00
问题 I'm tring to figure out how to represent friendships between users with Entity Framework (5) Code First. My initial idea was to create a class Friendship which contains references to two User instances, so that friendships are represented by separate objects. public class Friendship { public virtual int Id { get; set; } [Required] public virtual UserProfile User1 { get; set; } [Required] public virtual UserProfile User2 { get; set; } [Required] public virtual DateTime Since { get; set; } }

Entity Framework 5 poor performance

故事扮演 提交于 2019-12-01 20:25:10
I have 5 entities: public class Album { public int Id { get; set; } public string Title { get; set; } public virtual List<AlbumArtist> AlbumArtists { get; set; } public virtual List<Artist> Artists { get; set; } public virtual List<Genre> Genres { get; set; } public virtual List<Song> Songs { get; set; } } public class AlbumArtist { public int Id { get; set; } public string Title { get; set; } public virtual List<Album> Albums { get; set; } public virtual List<Artist> Artists { get; set; } public virtual List<Genre> Genres { get; set; } public virtual List<Song> Songs { get; set; } } public

The key component 'Id' is not a declared property on type 'TypeName', when using base class and private setter

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 19:24:42
I want to use an abstract base class for entities, which is not mapped on any table: public abstract class Entity { public virtual int Id { get; private set; } } Since Id will be auto-increment, I don't want to allow to change this property from outside. Hence, its setter is private . Here's a sample entity type: public class Order : Entity { public virtual string Customer { get; set; } } ...configuration types: public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity> where TEntity : Entity { public EntityConfiguration() { HasKey(o => o.Id); Property(o => o.Id)

How should I model friendships between users with EF code first?

走远了吗. 提交于 2019-12-01 18:55:24
I'm tring to figure out how to represent friendships between users with Entity Framework (5) Code First. My initial idea was to create a class Friendship which contains references to two User instances, so that friendships are represented by separate objects. public class Friendship { public virtual int Id { get; set; } [Required] public virtual UserProfile User1 { get; set; } [Required] public virtual UserProfile User2 { get; set; } [Required] public virtual DateTime Since { get; set; } } [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute

LazyLoadingEnabled setting doesn't seem to work in EF 5

China☆狼群 提交于 2019-12-01 18:31:48
问题 I'm using EF Model first with POCO entities and with custom DbContexts. My problem is that setting LazyLoadingEnabled=false does not affect anything, navigation properties are still loaded. Below is my example simplified. The entity Program. A program can be part of other programs: namespace Domain.Entities { using System; using System.Collections.Generic; public partial class Program { public Program() { this.Programs = new HashSet<Program>(); } public int Id { get; set; } public string

ASP .Net Entity Framework .tt Files Not Nesting Under .edmx

折月煮酒 提交于 2019-12-01 18:20:36
I'm running an ASP .NET (v4.5) Web Site Project under VS 2012 Update 2. When I create a new entity model (.edmx) under the App_Code folder, nested under the .edmx file is only the .Designer.cs and the .edmx.diagram files. The associated .tt files are not nested. I think this is why, when I save the model, the T4 templates are not automatically transformed, even though that option is specified in the model properties. Any ideas how to get this working? I cannot use the DependentUpon XML tag in a project file, since Web Site Projects do not have project files. Web Site does not support Nested

LazyLoadingEnabled setting doesn't seem to work in EF 5

那年仲夏 提交于 2019-12-01 18:18:11
I'm using EF Model first with POCO entities and with custom DbContexts. My problem is that setting LazyLoadingEnabled=false does not affect anything, navigation properties are still loaded. Below is my example simplified. The entity Program. A program can be part of other programs: namespace Domain.Entities { using System; using System.Collections.Generic; public partial class Program { public Program() { this.Programs = new HashSet<Program>(); } public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } public System.DateTime StartDate { get; set; }

The INSERT statement conflicted with the FOREIGN KEY constraint error

折月煮酒 提交于 2019-12-01 18:06:23
Hi I'm getting this error The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.Contacts_ContactID". The conflict occurred in database "aspnet-COGMakati-20140119015553", table "dbo.Contacts", column 'ContactID'. The statement has been terminated. I'm using Entity Framework and MVC 5's IdentityUser so I'm really lost on what I'm doing :| This is what I'm trying to populate: public class User : IdentityUser { [Required] [Display(Name = "First Name")] public string FirstName { get; set; } [Required] [Display(Name = "Last Name")] public string LastName { get; set;