ef-code-first

Pluggable Conventions in Entity Framework

烂漫一生 提交于 2019-12-06 12:57:38
I am following EF Feature CTP5: Pluggable Conventions to create custom pluggable convention (in this particular case to change the precision of all decimal fields). Looks like in the latest release of EF the Add method on ConventionsConfiguraions is also "internal'. How do i add custom Pluggable Conventions now? The feature has been removed in EF 4.1 and a possible implementation postponed to a later release: Code First customizable (pluggable) conventions are not supported. Removing the default Code First conventions is supported. Quote from here: http://msdn.microsoft.com/en-us/library

Define relationships for hierarchical data in Entity Framework Code First

旧巷老猫 提交于 2019-12-06 12:37:30
I have a hierarchical relationship defined for one of my tables where the relationship is stored in a separate join table. The join table also contains information about the type of relationship. The (simplified) schema looks like: Bills ID int IDENTITY(1,1) NOT NULL (PK) Code varchar(5) NOT NULL Number varchar(5) NOT NULL ... BillRelations ID int IDENTITY(1,1) NOT NULL (PK) BillID int NOT NULL RelatedBillID int NOT NULL Relationship int NOT NULL ... I have FK relationships defined on BillID and RelatedBillID to ID in the Bills table. I'm trying to map this in Entity Framework Code First with

Set identity to the previous created column during migration

风格不统一 提交于 2019-12-06 12:27:40
I have a project with the CodeFirst database (Entity Framework 6) and two migration steps. Database is updated automatically by using this code in Application_Start in Global.asax: Database.SetInitializer( new MigrateDatabaseToLatestVersion<MyDBEntities, MyNamespace.Configuration>()); First migration step is creating the tables: CreateTable( "dbo.GalleryAlbum", c => new { Id = c.Int(nullable: false), //other columns..... }) .PrimaryKey(t => t.Id); CreateTable( "dbo.GalleryPics", c => new { Id = c.Int(nullable: false), //other columns..... }) .PrimaryKey(t => t.Id) .ForeignKey("dbo.GalleryAlbum

How to properly implement Inheritance in EF, with performance in mind

喜夏-厌秋 提交于 2019-12-06 12:03:34
My app has Posts and Flags . Both of these have Responses , which have Comments . Post Response Comment Flag Response Comment I'm trying to model this in EF as simply and properly as possible, but I'm not sure if I'm going overboard here with inheritance. This is what I have: public class Post { [Key] [Required] public int PostId { get; set; } public virtual ICollection<PostResponse> Responses { get; private set; } } public class Flag { [Key] [Required] public int FlagId { get; set; } public virtual ICollection<FlagResponse> Responses { get; private set; } } I wanted to use just generic

Entity Framework - Non Key Relationships

自作多情 提交于 2019-12-06 12:00:57
问题 Problem I have a situation whereby I need to use Entity Framework 6, Code First, with a legacy database structure which cannot be changed. The database has a very generic table which stores text based data alongside some non key data which can be used to relate the record back to another table. To illustrate: Assume the Notes table has a model as follows: [Table("Notes")] public class Notes { [Key] public int RecordId { get; set; } [Required] public string RelatedTableName { get; set; }

How can i add extra column to third table in Entity Framework?

核能气质少年 提交于 2019-12-06 11:49:22
Assume i have two entities, User and Notification : public class User { [Key] public int UserId { get; set; } // other public properties // Navigation Properties public virtual ICollection<Notification> Notifications { get; set; } } public class Notification { [Key] public int NotificationId { get; set; } // other public properties // Navigation Properties public virtual ICollection<User> Users { get; set; } } I'm simply adding many-to-many Relationship like this: modelBuilder.Entity<User>() .HasMany(u => u.Notifications) .WithMany(t => t.Users) .Map(m => { m.ToTable("UserNotifications"); m

Object Mapping from stored procedure using the columnname attribute in EntityFramework CodeFirst

不想你离开。 提交于 2019-12-06 11:39:11
问题 I have an existing db that I am using entityframework 6 Code-First on to work with. A requirement though is that all work with the db has to be via stored procedures. So I started out using the mapping that is new in 6.0: modelBuilder.Entity<Post>().MapToStoredProcedures(); The issue is that this only supports mapping Insert, Update, and Delete sp's not the select sp's, I need to be able to us a sp to select. (I do not have access to edit any of the existing sp's) My poco's have attributes on

Many-to-many relationships using EF Code First

妖精的绣舞 提交于 2019-12-06 11:33:24
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID { get; set; } public string Title { get; set; } public bool Active { get; set; } public virtual ICollection<Question> Questions { get; set; } public virtual ICollection<Vendor> Vendors { get; set; } } public class Vendor { public int VendorID { get; set; } public string VendorName { get; set; } public virtual ICollection<Questionnaire> OpenQuestionnaires { get; set; } public virtual ICollection<Questionnaire> SubmittedQuestionnaires { get; set; } public virtual ICollection<QuestionnaireUser>

Entity Framework migrator's connection

柔情痞子 提交于 2019-12-06 11:15:14
Using the Entity Framework 5.0, I am attempting to manually migrate my code-first database during my MVC site's start up routine. To do this, I first create a DbContext instance and then run the following code: var migrator = new MigrateDatabaseToLatestVersion<DataContext, Configuration>(); migrator.InitializeDatabase(this.dataContext); I assumed the database associated with the dataContext's connection would be the one migrated. It appears though that this is not the case. Instead, it always tries to migrate a database on a local SQLExpress instance. There is an overload to the

EF Fluent API tutorial [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-06 09:41:08
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I want to learn Entity framework 4.1 Fluent API. Can you please send me goos links ? 回答1: The ADO.NET team blog has a lot of information on getting started. There's a good series of articles on the CTP5 release.