code-first

Entity Framework 6 one to many relationship

眉间皱痕 提交于 2021-01-28 08:14:53
问题 I develop web application with Entity framework 6. I have a model Folder that contains a list of Letter object. My letter object: public class Letter { [Key] public int Id [ForeignKey("Folder")] public int FolderId {get; set;} public virtual Folder Folder {get; set;} } I have 2 dbsets in my DbContext: DbSet<Folder> DbSet<Letter> My question is what happens when I add a new Letter? If I later on fetch the folder I added the letters, will those letters be contained in the folder's letters list?

Entity Framework - Is there a way to automatically eager-load child entities without Include()?

不打扰是莪最后的温柔 提交于 2020-06-30 03:56:57
问题 Is there a way to decorate your POCO classes to automatically eager-load child entities without having to use Include() every time you load them? Say I have a Class Car, with Complex-typed Properties for Wheels, Doors, Engine, Bumper, Windows, Exhaust, etc. And in my app I need to load my car from my DbContext 20 different places with different queries, etc. I don't want to have to specify that I want to include all of the properties every time I want to load my car. I want to say List<Car>

EF migration shows empty Up() Down() methods, no tables created in database

泄露秘密 提交于 2020-06-27 06:03:53
问题 EF migration shows empty Up() Down() methods _migrationhistory created in database but no tables has been created code for creating model Public partial class Student:BaseEntity { public string name { get; set; } public string collegeName { get; set; } public int numberOfBooks { get; set; } } code for model mapping public partial class StudentMap: EntityTypeConfiguration<Student> { public StudentMap() { this.ToTable("Students"); this.HasKey(c => c.Id); this.Property(c => c.name); this

Entity Framework 5 - code first array navigation property one to many with Interface Type

寵の児 提交于 2020-05-13 08:03:11
问题 These are my classes: public class Post : IPost { public int Id { get; set; } public virtual int[] DuplicateOf { get; set; } public virtual ICommentInfo[] Comments { get; set; } } public class CommentInfo : ICommentInfo { public virtual string Author { get; set; } public virtual int Id { get; set; } public virtual string Text { get; set; } public virtual int PostId{ get; set; } [ForeignKey("PostId")] public virtual Post Post { get; set; } } With this CommentConfiguration added to

Why is MVC3 not scaffolding my foreign key columns

泄露秘密 提交于 2020-02-01 08:17:21
问题 I'm trying to use MVC 3 with EF 4.1 using code first and am following Scott Guthries tutorial http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx. The issue I'm having is that when I create the products controller and the related scaffolded views, there is no "category" column being created in any of the views ("edit", "create", "index" etc), which according to the tutorial should be created. I've traced the reason why

Find a specified generic DbSet in a DbContext dynamically when I have an entity

若如初见. 提交于 2020-01-30 19:36:49
问题 I have following classes and DbContext : public class Order:BaseEntity { public Number {get; set;} } Product:BaseEntity; { public Name {get; set;} } public class Context : DbContext { .... public DbSet<Order> Orders { set; get; } public DbSet<Product> Products { set; get; } .... } I have a list of objects that want to add to my context, too, but I don't know how can I find appropriate generic DbSet according each entity type dynamically. IList<BaseEntity> list = new List<BaseEntity>(); Order

Incorrect value when saving enum

流过昼夜 提交于 2020-01-24 05:05:26
问题 I'm having a little difficulty getting Entity Framework 5 Enums to map to an integer column in a migration. Here's what the code looks like: [Table("UserProfile")] public class UserProfile { public enum StudentStatusType { Student = 1, Graduate = 2 } [Key] public int UserId { get; set; } public string UserName { get; set; } public string FullName { get; set; } public StudentStatusType Status { get; set; } } The migration looks like this: public partial class EnumTest : DbMigration { public

One-to-Zero-or-One relationship with E.F. Code First when column is not primary key

我的未来我决定 提交于 2020-01-23 17:18:08
问题 I'm developing an Entity Framework 6.1.3 Code First library, C# and .NET Framework 4.5.1. I have a problem with a One-to-Zero-or-One relationship (or maybe is another kind of relationship). I have two tables, Codes and HelperCodes , a code could have zero or one helper code. This is the sql script to create these two tables and their relationships: CREATE TABLE [dbo].[Code] ( [Id] NVARCHAR(20) NOT NULL, [Level] TINYINT NOT NULL, [CommissioningFlag] TINYINT NOT NULL, [SentToRanger] BIT NOT

How can I save an enumeration value to a database?

人盡茶涼 提交于 2020-01-23 09:29:09
问题 For example: namespace PizzaSoftware.Data { public class User { public string Username { get; set; } public string Password { get; set; } public Permission PermissionType { get; set; } } public enum Permission { Basic, Administrator } } If if I were to use Entity-Framework's CodeFirst, how can I save this value? This is for a Windows Forms, Desktop application. Thank you! 回答1: You can retrieve the int value (or whatever type you have your enum set to) via a simple cast and save that to the DB