entity-framework-4.1

Entity Framework Code First : Setting up One-To-One foreign key association using Annotations

孤者浪人 提交于 2019-11-26 11:25:52
问题 I have following two Entities that I am trying to relate (one to one) using foreign key associations. public class StandardRack { public int Id {get;set} public StandardRelay StandardRelay {get;set} } public class StandardRelay { public int Id {get;set} public int StandardRack_Id {get;set;} [Required][ForeignKey(\"StandardRack_Id\")] public StandardRack StandardRack { get; set; } } This throws ModelValidationException. Any ideas why such a seemingly simple one-to-one bidirectional

Ramifications of DbSet.Create versus new Entity()

一个人想着一个人 提交于 2019-11-26 11:05:50
问题 I am a bit confused about whether to use DbSet.Create, or simply new up an entity and add it. I don\'t really understand the ramifications of using DbSet.Create. I understand that DbSet.Create will create a proxied version if applicable, but I don\'t really understand what that means. Why do I care? It seems to me that an empty Proxied class is no more useful than a non-proxied class, since there are no related entities to lazy load. Can you tell me the difference, beyond the obvious? And why

Convert DBContext to ObjectContext for use with GridView

ぐ巨炮叔叔 提交于 2019-11-26 11:01:20
问题 I have a webforms project using EF codefirst to persist data. I\'d like to use a GridView and EntityDataSource, in order to save writing CRUD. Is this possible? Can I convert my DBContext to an ObjectContext that is expected by the EntityDataSource? Here\'s what I tried: <asp:EntityDataSource ID=\"OrdersDataSource\" runat=\"server\" ContextTypeName=\"SomeNamespace.Models.ShopDBContext\" EnableFlattening=\"False\" EntitySetName=\"Orders\" EntityTypeFilter=\"Order\" EnableDelete=\"False\"

How to filter nested collection Entity Framework objects?

我只是一个虾纸丫 提交于 2019-11-26 10:36:22
Here is the problem: I need to return a collection of objects with filtered nested collections. E.g: there is a store with orders and I need to return a collection with stores that includes nested collections with orders but without orders from customers that are marked as deleted. Here is what I try to do. But still no luck. Any suggestions are appreciated :) public List<StoreEntity> GetStores(Func<Store, bool> storeFilter, Predicate<OrderEntity> orderFileter) { IQueryable<StoreEntity> storeEntities = Context.Stores .Include(o => o.Order) .Include(cu => cu.Orders.Select(c => c.Customer))

How do you ensure Cascade Delete is enabled on a table relationship in EF Code first?

大城市里の小女人 提交于 2019-11-26 10:35:42
问题 I would like to enable CASCADE DELETE on a table using code-first. When the model is re-created from scratch, there is no CASCADE DELETE set even though the relationships are set-up automatically. The strange thing is that it DOES enable this for some tables with a many to many relationship though, which you would think it might have problems with. Setup: Table A <- Table B. Table B\'s FK points to Table A\'s PK. Why would this not work? 回答1: Possible reason why you don't get cascading delete

Composite Key with EF 4.1 Code First

眉间皱痕 提交于 2019-11-26 10:20:28
问题 I am trying to figure out how to have a composite key using EF code First 4.1 RC. Currently, I am using the [Key] Data Annotation, but I am unable to specify more than one key. how would one specify a composite key? Here is my Example: public class ActivityType { [Key] public int ActivityID { get; set; } [Required(ErrorMessage = \"A ActivityName is required\")] [StringLength(50, ErrorMessage = \"Activity Name must not exceed 50 characters\")] public string ActivityName { get; set; } } I need

How to map table splitting in EF Code First?

女生的网名这么多〃 提交于 2019-11-26 09:54:51
问题 How can I map table splitting with EF Code First? Table splitting for EDMX is described for example here. It allows mapping two entities with 1:1 relation into same table. I know I can do the similar mapping with entity and complex type but the big difference is that complex type can\'t be lazy loaded (or not loaded at all) which is the main reason for table splitting. 回答1: Here is how I just got EF 4.1 (RC) to do table splitting in Code First. Define your two entities. Make sure to include

Entity Framework 4.1 InverseProperty Attribute and ForeignKey

独自空忆成欢 提交于 2019-11-26 09:33:03
问题 I will create two references between Employee and Team entities with foreign keys. So I defined two entities as follow public class Employee { public int EmployeeId { get; set; } public string Name { get; set; } [ForeignKey(\"FirstTeam\")] public int FirstTeamId { get; set; } [InverseProperty(\"FirstEmployees\")] public virtual Team FirstTeam { get; set; } [ForeignKey(\"SecondTeam\")] public int SecondTeamId { get; set; } [InverseProperty(\"SecondEmployees\")] public virtual Team SecondTeam {

Entity Framework 4.1 InverseProperty Attribute

ε祈祈猫儿з 提交于 2019-11-26 09:25:17
问题 Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC. Does anyone know any useful resources about the scenarios that this attribute becomes useful? Should I use this attribute on navigation properties? example: public class Book { public int ID {get; set;} public string Title {get; set;} [ForeignKey(\"FK_AuthorID\")] public Author Author {get; set;} } public class Author { public int ID {get; set;}

Can you create sql views / stored procedure using Entity Framework 4.1 Code first approach

你离开我真会死。 提交于 2019-11-26 09:21:09
问题 Entity Framework 4.1 Code First works great creating tables and relationships. Is it possible to create sql views or stored procedure using Code first approach? Any pointers regarding this will be highly appreciated. Thanks a lot! 回答1: EF code-first approach expects that there is no logic in the database. That means no stored procedures and no database views. Because of that code-first approach doesn't provide any mechanism to generate such constructs automatically for you. How could it do