ef-code-first

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

Generate POCO classes and the mapping for an existing database using Entity Framework

限于喜欢 提交于 2019-12-24 00:44:00
问题 Is it possible to auto generate the POCO classes and the mapping with the database defined separately using Fluent API (instead of annotations) for an existing database? Instead of coding all these entity classes manually, I find it easier if they are auto generated and then I can change them as required if the names are not incorrect (plural or singular) or the some of the relationships are not correctly mapped etc. This will save lot of time for me compared to coding all the entity classes

EF 6 Code-Based Migration: Add not null property to existing entity

╄→гoц情女王★ 提交于 2019-12-24 00:19:28
问题 I want to add a not-null, foreing key column to an existing table. Environment: EF 6,Code-First, Code-Based Migration //Code from Migration class for new entity Currency CreateTable("dbo.Currency", c => new { CurrencyID = c.Int(nullable: false, identity: true), Code = c.String(nullable: false, maxLength: 3, fixedLength: true, unicode: false), Denomination = c.String(nullable: false, maxLength: 50, unicode: false), }) .PrimaryKey(t => t.CurrencyID); AddColumn("dbo.Collection", "CurrencyID", c

Invalid object name dbo.TableName

*爱你&永不变心* 提交于 2019-12-24 00:07:20
问题 I am getting the following error here: http://mvcbense.azurewebsites.net/ Invalid object name 'dbo.BlogPosts'. This is an MVC3 application using Entity Framework Code First. I have looked elsewhere and have only found something about changing the schema which I assume is what dbo. is. I am fairly new to EF, but it would look as if EF creates these for me, so I am unsure of what to do. Looking in my local SQL Server Express, all the tables have the dbo. schema on them. Why would I be able to

Entity Framework: Code First. Shared and Instance context

只谈情不闲聊 提交于 2019-12-23 23:35:29
问题 I'm building a layered application using Entity Framework code first approach with an mvc4 web application, separated mainly in Data , Services , and Web . From my web I do this: public void Foo() { EntityService _svc = new EntityService(); Entity = _svc.FindById(1); } The service method looks like this: private readonly MyContext _ctx = new MyContext(); public Entity FindById(long id) { return _ctx.Entities.SingleOrDefault(q => q.EntityId == id); } The problem is when I need to use more than

When using entity framework code-first mapping property to separate table, moves foreign key field

你离开我真会死。 提交于 2019-12-23 20:09:23
问题 Having odd problem with mapping tables with code-first EF. I have a parent class "Contract" with a many-1 relationship from another class "Supplier". The requirement came up to store a scanned copy of the contract within the Contract entity. To keep from having to query the document bytes every time, I want to store this property in a separate table. I can make EF store the ContractDocument in a separate table, but for some reason the FK to suppliers ends up in the ContractDocument table

Difficulty Concerning EF Code First Fluent API, TPH, and Foreign Keys

人走茶凉 提交于 2019-12-23 19:44:53
问题 I have two tables in my database. One is called Users , and the other is called Widgets . The Widgets table represents 3 entities in my code model. One of the entities, Widget , is a parent class for the other two entities, WidgetTypeA and WidgetTypeB . Both WidgetTypeA and WidgetTypeB have navigation properties to the User entity, which is persisted to the Users table in the database. I'm having trouble getting Code First to use the same foreign key for both the WidgetTypeA and WidgetTypeB

How to create/update a LastModified field in EF Code First

假装没事ソ 提交于 2019-12-23 18:45:07
问题 I want to add a column that stores the current DateTime when a record is saved to disk. What would be the best way to accomplish this? I know it's not a very complex problem, but I wondered if there is any best practice or any feature of EF that would simplify the task. For example: Is there any way to include the logic for this field inside the table Class, so that it's automatically updated whenever it's saved to disk? Is there any event to capture when an object is modified or saved? 回答1:

How can I optimize my EF Code First query?

雨燕双飞 提交于 2019-12-23 17:43:44
问题 [ Updated - see update at bottom ] I am using EF code-first, and am generally happy with it. However, one simple (and common) operation is causing EF to generate ridiculously complex SQL, which is slowing my application down. I am simply fetching a set of entities using a list of (integer) IDs, but because I need details of lots of sub-entities, I'm using .Include() to get those sub-entities loaded at the same time, as follows: db.MyEntities .Where(x => x.ClientId == clientId) .Where(x => ids

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