code-first

The operation failed because an index or statistics with name 'IX_ID' already exists on table 'TestAs'?

空扰寡人 提交于 2019-12-25 01:17:48
问题 I'm using EF4.3.1 code first, I have the code as below, namespace ConsoleApplication5 { class Program { static void Main(string[] args) { using (var context = new myContext()) { TestA ta = new TestA(); ta.Name = "Hero"; TestB tb = new TestB(); tb.Name = "Allen"; TestC tc = new TestC(); tc.Name = "Iverson"; ta.tb = tb; ta.tc = tc; context.testASet.Add(ta); } } } class TestA { public int ID { get; set; } public string Name { get; set; } public TestB tb { get; set; } public TestC tc { get; set;

Sequence contains no elements : self referencing code first

半城伤御伤魂 提交于 2019-12-25 00:37:01
问题 when I try to do self referencing with next entities public class Folder { public int Id { get; set; } public int? ParentFolderId { get; set; } // Added property public string Name { get; set; } public virtual ICollection<Folder> Childrens { get; set; } // Added property public virtual ICollection<ArticleTitle> ArticleTitles { get; set; } } public class ArticleTitle { public int Id { get; set; } public string Title { get; set; } public int? FolderId { get; set; } public virtual Folder Folder

Storing objects with Code First that are related as classes and subclasses

纵饮孤独 提交于 2019-12-24 19:07:21
问题 As an example I have an object Person, and Person is inherited by Man and Woman. Person contains the properties that both Man and Woman share, and Man and Woman contain the specific properties. What I wanna do is store those in a database using Entity Framework 4.1 Code First. So I made three model objects class Person { int Id { get; set; } string Name { get; set; } string Weight { get; set; } public virtual Woman Woman { get; set; } public int WomanID { get; set; } public virtual Man Man {

How to add existing collection of entities to existing entity without pulling first pulling existing entities from database using code-first framework

。_饼干妹妹 提交于 2019-12-24 10:39:44
问题 I'm trying to attach two existing and one new bid bid to an existing auction, but am unable to do so without first pulling the product from the database. This code works, but how would I go about just providing the bid id's for the existing bids and then save the auction using (var db = new DataContext()) //find existing auction var auction = db.Auctions.Find(1); var bid1 = db.Bids.Find(1); var bid2 = db.Bids.Find(2); var bid3 = new Bid() { //New Bid }; //existing auction.Bids.Add(bid1); /

How do I setup a foreign key relationship in codefirst without using a navigation property?

梦想与她 提交于 2019-12-24 00:48:39
问题 Say you have an order class with an order status, I want to declare the OrderStatusId inside the OrderStatus class. However, no foreign key relationship is set-up by default. If I use [ForeignKey] attribute on the column it seems to demand a navigation property which I don't want as this would mean having to carry out joins on the navigation property in all of my queries just to check the status. How do I accomplish this in EF codefirst? Define a property as a foreign key without using a

Entity Framework Adding Existing Item Causes Clone

人盡茶涼 提交于 2019-12-23 23:25:37
问题 I'm using the Entity Framework v4 for a small project. Normally I use NHibernate. My issue is that I accidentally had code which added an object which was already persisted to the DB Context's collection, and when I did a SaveChanges(), the EF made a copy of the object, giving it a new Primary Key. While this is useful, it's not what I wanted. Is there a way to turn off that functionality, and instead have an exception thrown? UPDATE: CODE NOW INCLUDED public class CcUser { public int Id {

Cannot rename Discriminator column in Entity Framework 4.1 Code First database

前提是你 提交于 2019-12-23 16:26:14
问题 I have a model hierarchy configuration like so: [Table("A")] abstract class A { } class B : A { } // treated as abstract [Table("C")] class C : B { } This results in a TPH for A and B, and a TPT for C. So far this seems to work fine. There are two database tables being generated: "A" which contains both A and B model records, and "C" which contains only C model record columns not already kept in table "A". For the TPH arrangement on Table A, there is a generated "Discriminator" column, which

EF 4.1 Code First bug in Find/Update. Any workaround? Should it be reported?

扶醉桌前 提交于 2019-12-23 15:38:21
问题 I've found a pretty nasty bug in EF 4.1 Code First. Assume we have this piece of code to retrieve an entity from the context and then update it with new values: public T Update<T>(T toUpdate) where T : class { System.Data.Objects.ObjectContext objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)_context).ObjectContext; System.Data.Objects.ObjectSet<T> set = objectContext.CreateObjectSet<T>(); IEnumerable<string> keyNames = set.EntitySet.ElementType .KeyMembers .Select(k

In EF4 Code first Collection are not lazy loading?

为君一笑 提交于 2019-12-23 15:14:08
问题 Using Microsoft Visual C# 2010 Express, Entity Framework Feature CTP4. I tried EF4 with code first with something small based on Scott Gu's blog. But it seems that collections are not initialized when retrieving an entity. I get a null reference exception when adding a product to a category. In all the examples I've seen, the collection are never explicitly initialized. What am I missing? Here's my code: using System; using System.Collections.Generic; using System.Linq; using System.Text;

One to Many in Code First Entity Framework model - How do the relationships work?

*爱你&永不变心* 提交于 2019-12-23 15:13:34
问题 Given the standard Northwind database (reduced for conciseness): [Table("Order Details")] public class OrderDetail : IValidatableObject { [Key, Column(Order = 1)] public int OrderID { get; set; } [Key, Column(Order = 2)] public int ProductID { get; set; } ... } [Table("Orders")] public class Order { public int OrderID { get; set; } ... public virtual ICollection<OrderDetail> OrderDetails { get; set; } } I'm wondering about how this works exactly. I'm guessing that because the primary key in