ef-code-first

Entity Framework migrator's connection

梦想的初衷 提交于 2019-12-07 20:06:31
问题 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

How to view the EF Code First Migrations changes before calling 'Update-Database'

被刻印的时光 ゝ 提交于 2019-12-07 19:21:19
问题 I am using Code First Migrations Beta 1. I would like to see the Migrations and SQL that will run before I actually call 'Update-Database' on the Package Manager Console. Is there a command to get this information on the Package Manager Console? 回答1: Take a look at the link below and search for the section headed Generating an SQL Script. ADO.Net Team Blog Run the Update-Database –TargetDatabase:"database" –Script command in Package Manager Console 来源: https://stackoverflow.com/questions

Many-to-many relationships using EF Code First

无人久伴 提交于 2019-12-07 18:48:13
问题 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

Save detached object graph using Entity Framework code first causes Primary Key violation

孤人 提交于 2019-12-07 18:45:44
问题 I'm trying to save an object graph of POCOs I have mapped to EF6 using Code First fluent notations. Upon saving the object graph however, I stumble upon primary key violation exceptions. The object graph is quite simple: One Issue can contain multiple WorkItems with each one Author (as User ). The objects are populated externally (using a Web API) When I attempt to save an issue with two workitems which refer to the same author, I would expect the issue to be inserted, the workitems to be

Entity Framework / code first / table-per-type inheritance - implementing a one-to-many association between a derived class and a concrete class

天大地大妈咪最大 提交于 2019-12-07 17:09:24
问题 I'm using MVC4 in VS2012, and I'm following a table-per-type inheritance approach. I'm trying to use the Seed() method to add data to my database. I have the following classes: LandLord [Table("Landlord")] public class Landlord : UserProfile { // A Landlord can have many ResidentialProperties [ForeignKey("ResidentialPropertyId")] public virtual ICollection<ResidentialProperty> ResidentialProperties { get; set; } } ResidentialProperty [Table("ResidentialProperty")] public class

Get ignored properties in Entity Framework

你。 提交于 2019-12-07 17:04:23
问题 I work on a framework with EF. I want to get all ignored properties of an entity to build some special queries. How can I do it? public class Customer { public int Id { get; set; } public DateTime BirthDate { get; set; } public int Age { get; set; } } public class CustomerContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Customer>().Ignore(customer => customer.Age); base.OnModelCreating(modelBuilder); } public DbSet<Customer>

Referencing EntityFramework 4.1 namespace in MVC3 Razor View

一曲冷凌霜 提交于 2019-12-07 15:57:15
问题 I am trying to reference the System.Data.Entity.Validation (EF 4.1 version) namespace inside of a shared View in my MVC3 project. I've been able to reference other external libraries using: @using Example.Namespace I cannot, however, get the same thing to work when it comes to libraries that are part of the new 4.1 EntityFramework. I have tried adding the following to the web.config within the Views folder: <add namespace="System.Data.Entity.Validation, EntityFramework, Version=4.1.10715.0,

Code First Entity Framework Many-to-Many relationships

こ雲淡風輕ζ 提交于 2019-12-07 15:22:17
问题 Can someone point out where I've got wrong!! I have created 2 simple classes, with many-to-many relationship. Works fine, all the tables are populated correctly. Except when I try and retrive any Students courses nothing is returned... public partial class Student { public Student() { Courses = new HashSet<Course>(); } public int StudentID { get; set; } [StringLength(50)] [Required] public string FirstName { get; set;} [StringLength(50)] [Required] public string LastName {get; set;} public

EF 4.3 (Code First) - determine when items are added to the virtual ICollection property

人盡茶涼 提交于 2019-12-07 15:06:57
问题 Is there any way to determine when actual items are added to an ICollection<> virtual member when it is being loaded from a query? Hopefully the code below will be able to demonstrate my point!! public class DbAppointment { public DbAppointment() { } public virtual int AppointmentId { get; set; } public virtual string Subject { get; set; } public virtual string Body { get; set; } public virtual DateTime Start { get; set; } public virtual DateTime End { get; set; } private ICollection

One-to-one relationship in CF Entity Framework using mapping

℡╲_俬逩灬. 提交于 2019-12-07 14:42:10
问题 I'm using Entity Framework 5, Code-First. I've two domain objects (or tables). 1st is User , and 2nd is UserProfile . One user can have only one profile, and one profile belongs to only one user. That is 1-1 relationship. Here are the classes.... (I simplified the code to make it understandably, It is actually more complex) User public class User { public virtual Int64 UserId { get; set; } public virtual UserProfile UserProfile { get; set; } public virtual String Username{ get; set; } public