code-first

Entity Framework Find method not working properly

我们两清 提交于 2019-12-01 05:53:26
问题 I have classes called Course, Student and Teacher like this public class Course { [Key, DatabaseGenerated(DatabaseGenerationOption.Identity)] public Guid CourseId { set; get; } public ICollection<Student> Students { set; get; } public Teacher Teacher { get; set; } } public class Student { [Key, DatabaseGenerated(DatabaseGenerationOption.Identity)] public Guid StudentId { set; get; } public ICollection<Course> Courses { set; get; } } public class Teacher { [Key, DatabaseGenerated

more than one navigation to the same entity

╄→гoц情女王★ 提交于 2019-12-01 05:34:42
问题 I have a problem with connection between 2 entities when there is 2 navigations. to be specific, I have the following classes: public class TableA { public TableA() { ListBs = new List<TableB>(); } [Key] public int Id { get; set; } public TableB MainB { get; set; } public virtual ICollection<TableB> ListBs { get; set; } } public class TableB { [Key] public int Id { get; set; } public virtual TableA refA { get; set; } [Required] public string Text { get; set; } } The scenario of this

Entity Framework Automatic Migrations Existing Database

久未见 提交于 2019-12-01 05:12:04
问题 I am building an ASP.Net MVC4 web application with Entity Framework 5. I had to use an existing sql server database but also wanted to use Code First, so I followed this tutorial http://msdn.microsoft.com/en-us/data/jj200620.aspx My application uses automatic migrations with Entity Framework. The version of sql server I was using throughout the development phase was 2008, however, at the last minute I've been told the database needs to work on sql server 2005. I got the database I was using

Entity framework 4.3 with required association

删除回忆录丶 提交于 2019-12-01 04:02:37
问题 I'm a very strange behavior with EF code first approach and associations. I have two entities: public class GlobalKpiSectionn { public GlobalKpiSection() { this.Regions = new HashSet<Region>(); } public virtual ICollection<Region> Regions { get; protected set; } } public class Region { public int RegionId { get; set; } public bool IsMain { get; set; } [Required] public virtual GlobalKpiSection KpiSection { get; set; } } I need required attribute on KiSection property in order to get cascade

Entity Framework Circular Reference

 ̄綄美尐妖づ 提交于 2019-11-30 22:58:44
问题 Trying this question again because my first attempt was barely coherent :p So I am super confused and using Entity Framework Code First I have a Forest class. I have a Tree class. Each Forest can have many Trees When I was trying to serialize I was getting circular reference public class Forest { public Guid ID { get; set; } public virtual List<Tree> Trees { get; set; } } public class Tree { public Guid ID { get; set; } public Guid? ForestId {get;set;} [ForeignKey("ForestId")] public virtual

EF 4.1: Why does turning a constant into a variable result in extra sub query?

南笙酒味 提交于 2019-11-30 22:22:28
Today I discovered that Entity Framework was adding an unnecessary sub query to the SQL it generates. I started digging my code trying to narrow down where it might come from. A (long) while later I pin-pointed what's causing it. But now I'm more confused than when I started, as I have no clue why it causes it. Basically what I discovered is that on certain scenarios, simply converting a constant into a variable can alter the SQL that Entity Framework generates. I've shrunk everything to the bare minimum and packed it in a little console app: using System; using System.Data.Entity; using

how to do many to many with the same table with EF4 Code First

拈花ヽ惹草 提交于 2019-11-30 18:58:16
I have this schema: create table Person ( id int identity primary key, name nvarchar(30) ) create table PersonPersons ( PersonId references Person(id), ChildPersonId references Person(id) ) how to create the classes to map them using EF4 Code First CTP5 ? Brian Kretzler For the POCO... class Person { public Guid PersonId { get; set; } public virtual Person Parent { get; set; } public virtual ICollection<Person> Children { get; set; } } ...set up the mapping in the DbContext... protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Person>() .HasOptional(entity

Relationships in Entity Framework Code First

和自甴很熟 提交于 2019-11-30 18:04:08
问题 yesterday I created database in Management Studio and now I want to create it in program using EF Code First. Here is link to my database: http://s11.postimg.org/6sv6cucgj/1462037_646961388683482_1557326399_n.jpg And what I did: public class GameModel { [Key] public int Id { get; set; } public string Name { get; set; } public DateTime CreationTime { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public string TotalTime { get; set; } public DateTime

EF 4.1: Why does turning a constant into a variable result in extra sub query?

旧时模样 提交于 2019-11-30 17:53:06
问题 Today I discovered that Entity Framework was adding an unnecessary sub query to the SQL it generates. I started digging my code trying to narrow down where it might come from. A (long) while later I pin-pointed what's causing it. But now I'm more confused than when I started, as I have no clue why it causes it. Basically what I discovered is that on certain scenarios, simply converting a constant into a variable can alter the SQL that Entity Framework generates. I've shrunk everything to the

Many-many relationship in Entity Framework Code First and using the “virtual” keyword to access each other

二次信任 提交于 2019-11-30 17:20:18
问题 This excerpt code successfully creates a many-many relationship with an explicit Junction table that has additional data within it. PROBLEM: I want to be able to access Courses from Student and vice versa, (therefore the commented virtual property. But if I uncomment it, it causes an error (see below)) If I don't explicitly create a junction table (no additional data), the virtual keyword works though as EF creates a junction table by convention. QUESTION: How can I make Student access