code-first

Entity Framework Code First One to One Relationship

北慕城南 提交于 2019-12-02 17:46:08
I have the following two models public class Account { public int Id { get; set; } public string Name { get; set; } public int CurrentPeriodId { get; set; } [ForeignKey("CurrentPeriodId")] public virtual Period CurrentPeriod { get; set; } public virtual ICollection<Period> Periods { get; set; } } public class Period { public int Id { get; set; } public int AccountId { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public virtual Account Account { get; set; } } And I am trying to run the following query: from p in context.Periods where p.AccountId ==

Storing Data in Session State for a Shopping Cart

橙三吉。 提交于 2019-12-02 16:19:52
问题 trying to take what I have stored adding something to a cart into a session then transferring it to another page to get a GridView to show up of all the items that I have added onto the Cart session. Storing it as an Object Session. -AddToCart takes that rows details and stores in A Session then takes that session object and displays it on a Grid view on another page. takes this code from it: protected void GridViewDisplay_RowCommand(object sender, GridViewCommandEventArgs e) { if (e

Entity Framework inserting duplicates on Seeding database [duplicate]

我只是一个虾纸丫 提交于 2019-12-02 14:56:33
问题 This question already has an answer here : Entity Framework database mapping relationships (Duplicate creation using Seed() method) (1 answer) Closed 12 months ago . EDIT---- From here i tried assigning Id's in the seeding method and this was OK for Languages but not when i added Address to the customer and assign Id's as well to these addresses, than it created the dupes again... Both Address & Language are declared as DbSet<...> in my Context What i tried: Adding 1 Address (with Id) - add

Entity framework one to zero or one relationship without navigation property

眉间皱痕 提交于 2019-12-02 14:29:45
问题 I hit an issue when trying to delete records due to FK constraints. I therefore went back to the drawing board and am trying to specify how the relationship should work. Here are my code first classes: public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int? DeferredDataId { get; set; } [ForeignKey("DeferredDataId")] public virtual DeferredData DeferredData { get; set; } } public class DeferredData { [Key]

Entity Framework 4.1 RC (Code First) - Entity not updating over association

五迷三道 提交于 2019-12-02 13:35:06
问题 What I'm trying to do is fairly simple. I have two classes: public class TownRecord { public int Id { get; set; } public string ShortName { get; set; } public string FileName { get; set; } public string tags { get; set; } public virtual TownRecordType RecordType { get; set; } public DateTime? DateScanned { get; set; } public DateTime? RecordDate { get; set; } [StringLength(4000)] public string Comments { get; set; } public string UploadedBy { get; set; } } public class TownRecordType { public

How to specify two navigation properties from entity X to the same target entity, Y?

天涯浪子 提交于 2019-12-02 13:31:05
Consider that I have an Instructor class: public class Instructor { public InstructorTypesEnum Type { get; set; } public virtual ICollection<Course> Courses { get; set; } public virtual ICollection<Course> CoInstructingCourses { get; set; } } Here, InstructorTypesEnum has two values: instructor and co-instructor. So the two navigation properties Courses and CoInstructingCourses are supposed to return those different courses. Of course I am also having difficulties specifying the coresponding properties in the Course class public Guid InstructorId { get; set; } public virtual Instructor

Entity Framework inserting duplicates on Seeding database [duplicate]

こ雲淡風輕ζ 提交于 2019-12-02 08:31:48
This question is an exact duplicate of: Entity Framework database mapping relationships (Duplicate creation using Seed() method) 1 answer EDIT---- From here i tried assigning Id's in the seeding method and this was OK for Languages but not when i added Address to the customer and assign Id's as well to these addresses, than it created the dupes again... Both Address & Language are declared as DbSet<...> in my Context What i tried: Adding 1 Address (with Id) - add this to 1 customer => Creates a dupe Adding 1 language & 1 Address (with Id's) - add both to 1 customer => Creates a dupe Adding 1

EF6 entity with DatabaseGeneratedOption.Identity Guid Id force insert my Id value

蹲街弑〆低调 提交于 2019-12-02 03:23:00
I am trying to use EF to export/import the existing database of a DbContext. In this context, there are several entities with Guid Id properties with DatabaseGeneratedOption.Identity defined by the ModelBuilder. When I re-import the entities, I want to use the Id value from the serialized object, but it always generates a new Id value when I save the changes. Is there any way to force EF to use my Id value in this case? I know DatabaseGeneratedOption.None will allow me to do it, but then I will always be responsible for generating the Id. I know there segmentation issues of the index that

Entity framework one to zero or one relationship without navigation property

爷,独闯天下 提交于 2019-12-02 03:22:05
I hit an issue when trying to delete records due to FK constraints. I therefore went back to the drawing board and am trying to specify how the relationship should work. Here are my code first classes: public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int? DeferredDataId { get; set; } [ForeignKey("DeferredDataId")] public virtual DeferredData DeferredData { get; set; } } public class DeferredData { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } //other properties

Entity Framework 4 Code First: Where is my database?

二次信任 提交于 2019-12-02 01:14:44
问题 Creating a web site using code first, for my own piece of mind i would like to be able to explore the db it creates like you can when you use database first and create the SQL server db within VS2010. I have had a look around, and the only related information i can find seems to be using a previously existing db with code first, I want the db to be made by code first and i just want to know where it is, or how to change the default location of it so i can look in it. I have had a fiddle with