ef-code-first

Define relationships for hierarchical data in Entity Framework Code First

ぐ巨炮叔叔 提交于 2019-12-08 02:36:22
问题 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

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

半腔热情 提交于 2019-12-08 02:06:11
问题 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>()

Set identity to the previous created column during migration

巧了我就是萌 提交于 2019-12-08 02:01:15
问题 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

Pluggable Conventions in Entity Framework

♀尐吖头ヾ 提交于 2019-12-08 01:46:39
问题 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? 回答1: 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

How to add computed column using migrations in code first?

旧街凉风 提交于 2019-12-08 01:40:50
问题 I have added this computed column inside my data model [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public string FullName { get; private set; } After that I created it inside my database using this query ALTER TABLE [MyDataBase].[dbo].[User] ADD FullName as ([FirstName] + ' ' + [LastName]) When I run my code I get an error that my database has changed . My question How to create migration for this computed column (because it's already created using sql query) 回答1: Entity Framework

Why lazy loading does not work in razor views (cshtml files)?

一曲冷凌霜 提交于 2019-12-08 01:39:23
问题 I write following statements in my cshtml file:- @{ string categoryName = string.Format("{0}->{1}", label.Category.Parent.Name, label.Category.Name); @categoryName } and get an exception Object reference null . On the other hand the same works in the Controller.cs file and in the immediate window too. I know it is related to lazy loading. Is there any concept behind it, that it does not work in Expressions in Cshtml files ? Thanks 回答1: This doesn't work because your context is disposed by the

Is it possible perform actions in POCO before insert/update?

a 夏天 提交于 2019-12-08 01:18:28
Let's assume this Entity Framework sample: public class User { public int UserID { get; set; } public string Name { get; set; } } public class MyDbContext : DbContext { public DbSet<User> Users { get; set; } } class Program { static void Main(string[] args) { using (var db = new MyDbContext ()) { var user = new User { Name = "Foo"}; db.Users.Add(user); db.SaveChanges(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } } I want add to User class events, like "BeforeInsert" or "BeforeUpdate", and when the code... db.Users.Add(user); ... is executed anywhere in the

Entity Framework one-to-zero-or-one foreign key association

橙三吉。 提交于 2019-12-08 00:49:35
问题 I am in the process of changing the back-end of an existing application to use Entity Framework Code First. I've used the built-in tool in Visual Studio 2015 to generate POCO classes based on my existing database. This worked perfectly for the most part, except for two classes, with a one-to-zero-or-one relationship. These are my (simplified) classes: public class Login { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int TeamMemberId { get; set

EF Inheritance and Foreign Keys

≡放荡痞女 提交于 2019-12-07 22:48:19
问题 I'm going for TPH inheritance here - one Comments table with a discriminator. I keep getting the error: The foreign key component 'ResponseId' is not a declared property on type 'PostComment'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property. Whenever I try to perform a fresh migration. Models: public class Comment : Message { [Key] [Required] public int CommentId { get; set; } [Required] public int ResponseId { get; set; } } public

Entity Framework RC1 DbContext query issue

邮差的信 提交于 2019-12-07 21:08:06
问题 I'm trying to implement the repository pattern using entity framework code first rc 1. The problem I am running into is with creating the DbContext. I have an ioc container resolving the IRepository and it has a contextprovider which just news up a new DbContext with a connection string in a windsor.config file. With linq2sql this part was no problem but EF seems to be choking. I'll describe the problem below with an example. I've pulled out the code to simplify things a bit so that is why