ef-code-first

How do I save a child entity in EntityFramework 4?

耗尽温柔 提交于 2019-12-12 08:10:26
问题 I have a 1-1 relationship between Orders and Contact. i.e. Contact.OrderId references Orders and is also a PK. So I have an existing Order and I add a new Contact to it like so... order.Contact = new Contact() { EmailAddress = "hello" }; context.Orders.Attach(order); context.SaveChanges(); A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship. So what am

MVC .Net Cascade Deleting when using EF Code First Approach

耗尽温柔 提交于 2019-12-12 07:47:22
问题 I'm pretty new to MVC and i'm having troubles with cascade deleting. For my model I the following 2 classes: public class Blog { [Key] public int Id { get; set; } [Required] public string Name { get; set; } [DisplayFormat()] public virtual ICollection<BlogEntry> BlogEntries { get; set; } public DateTime CreationDateTime { get; set; } public string UserName { get; set; } } public class BlogEntry { [Key] public int Id { get; set; } [Required] public string Title { get; set; } [Required] public

EF Code First - How to prevent duplicate column on created table on Table-Per-Hierarchy Mapping

旧街凉风 提交于 2019-12-12 06:29:25
问题 I have entity like below public abstract class MyBaseClass { public int Id { get; set; } } public class MyConcrete : MyBaseClass { public int TemplateName { get; set; } public int Total { get; set; } } public class MyOtherConcrete : MyBaseClass { public int TemplateName { get; set; } public int MyProperty { get; set; } public string OtherProperty { get; set; } } using default initialization, EF will make table with columns like bellow: Id TemplateName TemplateName1 // <-- this is the problem

Entity Framework 6 Lazy Loading not working

北慕城南 提交于 2019-12-12 06:28:00
问题 I am having some trouble with EF6 lazy loading, code first to an existing database. Here are the Entities that are giving me the issue, I have no idea why it is not working, everything I find online says it should be working. public class User { public long UserId { get; set; } public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection<Token> Tokens { get; set; } public virtual ICollection<Business> Businesses { get;

Entity framework multiple composite keys, cascade on delete

女生的网名这么多〃 提交于 2019-12-12 05:57:32
问题 I'm having a bit of a problem with configuring composite keys in Entity framework between 3 tables, code first approach. I have a base class that has the Id, which all of my classes inherit from. The first table has a collection of the second table items, while it has a collection of the third table. I need composite keys for cascade on delete when removing an element from either table. I am also using the aggregate root pattern. public abstract class BaseClass { [Key, Column(Order = 0)]

Entity Framework reverts changes [closed]

若如初见. 提交于 2019-12-12 05:29:58
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I have MVC application hosted as Azure web role and I also have Worker role which checks some data and update records in database. Worker role checks data on every 15 minutes. Yesterday, I went into big trouble because a lot of changes made via MVC application is just reverted. I will try to give an example:

Entity Framework Code First unique index foreign key throws DbUpdate Exception

时光怂恿深爱的人放手 提交于 2019-12-12 05:29:38
问题 Here's the entites and context code: public class Family { public int FamilyID { get; set; } public String address { get; set; } public virtual ICollection<Member> FamilyMembers { get; set; } } public class Member { [Index("MemberUniqueID", IsUnique = true)] public int MemberID { get; set; } [StringLength(50)] [Index("MemberUniqueIndex", 1, IsUnique = true)] public String name { get; set; } [StringLength(50)] [Index("MemberUniqueIndex", 2, IsUnique = true)] public String surname { get; set; }

How to map two One or Zero to One relationships

我是研究僧i 提交于 2019-12-12 05:06:02
问题 I have three entities with the name of SellinRequest, MortgageAndRent and Album. Each of SellinRequest and MortgageAndRent may have an Album. public class SellingRequest { public int Id { get; set; } public Album Album { get; set; } } public class MortgageAndRent { public int Id { get; set; } public Album Album { get; set; } } public class Album { public int Id { get; set; } public SellingRequest SellingRequest { get; set; } public int SellingRequestId { get; set; } public MortgageAndRent

EF4.3 create one to one or zero relationship failed

こ雲淡風輕ζ 提交于 2019-12-12 05:05:35
问题 I use EF4.3 to create 1 to 1...0 relationship, but it throw an exception of "The operation failed because an index or statistics with name 'IX_id' already exists on table 'TestAs'" The code as below namespace ConsoleApplication1 { class Program { static void Main(string[] args) { using (myContext context = new myContext()) { TestA tA = new TestA(); TestB tB = new TestB(); TestC tC = new TestC(); context.testA.Add(tA); context.testB.Add(tB); context.testC.Add(tC); context.SaveChanges(); } } }

Relationship mapping table with additional properties?

点点圈 提交于 2019-12-12 05:02:34
问题 I have three classes: public partial class Student : Contact { //Inherited from Contact: //public int ContactId { get; set; } //public string FirstName { get; set; } //public string LastName { get; set; } public virtual StudentExam StudentExam { get; set; } } public partial class Exam { public int ExamId { get; set; } public string Title { get; set; } public DateTime Date { get; set; } public virtual StudentExam StudentExam { get; set; } } public partial class StudentExam { public byte Score