entity-framework-4.1

Using the Entry<TEntity>().CurrentValues.SetValues() is not updating collections

不羁的心 提交于 2019-11-27 14:58:44
I have not run into this before, because I usually handled collections by them selves instead of modifying them directly on the entity. public class Schedule: BaseEntity { public Guid Id {get;set;} public virtual int? DayOfTheWeekTypeId { get; set; } public virtual DayOfTheWeekType DayOfTheWeekType { get; set; } public virtual ICollection<Instructor> Instructors { get; set; } public DateTime? StartDateTime { get; set; } public DateTime? EndDateTime { get; set; } public string SpecialInstructions { get; set; } } Mapping class: public ScheduleMapping() { HasMany(c => c.Instructors).WithMany()

Entity Framework Code First Many to Many Setup For Existing Tables

放肆的年华 提交于 2019-11-27 14:43:35
I have the following tables Essence , EssenseSet , and Essense2EssenceSet Essense2EssenceSet is the linking table that creates the M:M relationship. I've been unable to get the M:M relationship working though in EF code first though. Here's my code: [Table("Essence", Schema = "Com")] public class Essence { public int EssenceID { get; set; } public string Name { get; set; } public int EssenceTypeID { get; set; } public string DescLong { get; set; } public string DescShort { get; set; } public virtual ICollection<EssenceSet> EssenceSets { get; set; } public virtual EssenceType EssenceType { get;

Entity Framework 4.1 DbSet Reload

走远了吗. 提交于 2019-11-27 14:25:46
I'm using a single instance of DbContext scenario to shadow entire copy of the database locally in a WPF app. I've heard this is bad practice, but my database is small and I need an entire copy of it locally while the app is running. An extension method for IQueryable , Load() lets me preload the elements of a DbSet<> , so that I can bind things to the Local property of DbSet<> . Data in the database changes rapidly, so I want to SaveChanges() and reload everything , even objects that are already tracked. Calling the Load() method again doesn't update the items that are tracked but are not

What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

假装没事ソ 提交于 2019-11-27 14:02:46
In EF 4.1+, is there a difference between these 2 lines of code? dbContext.SomeEntitySet.Add(entityInstance); dbContext.Entry(entityInstance).State = EntityState.Added; Or do they do the same thing? I'm wondering if one might affect child collections / navigation properties differently than the other. When you use dbContext.SomeEntitySet.Add(entityInstance); the status for this and all its related entities/collections is set to added, while dbContext.Entry(entityInstance).State = EntityState.Added; adds also all the related entities/collections to the context but leaves them as unmodified. So

Set database collation in Entity Framework Code-First Initializer

↘锁芯ラ 提交于 2019-11-27 14:01:21
I want to set the default collation for a database, when Entity Framework Code First creates it. I've tried the following: public class TestInitializer<T> : DropCreateDatabaseAlways<T> where T: DbContext { protected override void Seed(T context) { context.Database.ExecuteSqlCommand("ALTER DATABASE [Test] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"); context.Database.ExecuteSqlCommand("ALTER DATABASE [Test] COLLATE Latin1_General_CI_AS"); context.Database.ExecuteSqlCommand("ALTER DATABASE [Test] SET MULTI_USER"); } } This appears to run OK when SQL Server is already set to the same default

Model First with DbContext, Fails to initialize new DataBase

一曲冷凌霜 提交于 2019-11-27 13:57:18
I give up. I found this: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx And thought, that's cool. So I quickly redesigned my model to take advantage of best of two worlds. But now my model fails at creating new database (or adding tabls to existing one). I get this error: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of

Loading Nested Entities / Collections with Entity Framework

瘦欲@ 提交于 2019-11-27 13:45:16
问题 I am trying to Eagerly load all the related entities or collection of Entity in one call. My Entities Looks like: Class Person { public virtual long Id { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } } Class Employee { public virtual long Id { get; set; } public DateTime AppointmentDate { get; set; } public virtual ICollection<EmployeeTitle> Titles { get; set; } public virtual Person Person { get; set; } } Class EmployeeTitle { public

An overflow occurred while converting to datetime using EF4

不羁的心 提交于 2019-11-27 13:37:09
问题 I have a windows application working with a SQL Compact 4.0 database, using EF 4.1 and code-first approach. I cannot save an object to the database since I'm getting an exception, with inner exception "An overflow occurred while converting to datetime" when trying to save the type Quotation: public class Quotation { public int ID { get; set; } public string Name { get; set; } public DateTime DateCreated { get; set; } public ContactPerson ContactPersonAssigned { get; set; } public string

Understanding code first virtual properties

大兔子大兔子 提交于 2019-11-27 13:08:27
Hi I am just learning to work with Entity Framework Code First and I can not seem to understand something.I have created three models based on a tutorial: public class Course { public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } public virtual ICollection<Enrollment> Enrollments{ get; set; } } public class Enrollment { public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public decimal? Grade { get; set; } public virtual Course Course { get; set; } public virtual Student Student { get; set; }

Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

≡放荡痞女 提交于 2019-11-27 13:01:37
问题 I'm stuck at trying to write the Entity Framework 4.1 code first model for the following DB relationship. Here is a visual of the relationship. dbo.[Companies] can have either Seller or Debtor as Company Types. dbo.[SellerDebtors] defines the connection a Seller Company has with a Debtor Company. The code i've written is based on my original EF 4.0 POCO model code. This is what I've come up with - This code does not work. public class SellerDebtor { public int SellerDebtorId { get; set; }