Enums with EF code-first – standard method to seeding DB and then using?

Unfortunately, enums are not natively supported on EF 4.1. Here’s one rather known article on how to deal with them: Faking enums on EF 4. It does, however, require a wrapper. There’s a simpler way to map enums in EF 4 however: just create an int property on your class to represent the int value … Read more

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

One-to-one foreign key associations are not supported by Entitiy Framework. You must remove the foreign key and use shared primary keys (primary key of the dependent is its foreign key to the principal at the same time): public class StandardRack { public int Id {get;set} public StandardRelay StandardRelay {get;set} } public class StandardRelay { public … Read more

How do I define a database view using Entity Framework 4 Code-First?

That’s because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can’t define such constructs using code first. If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer – … Read more

Entity 4.1 Updating an existing parent entity with new child Entities

I first clear the existing ingredients collection in the product entity and than add the updated list of ingredients again. Well, this is kind of brute-force-attack to update the child collection. EF doesn’t have any magic to update the children – which means: adding new children, deleting removed children, updating existing children – by only … Read more

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

Possible reason why you don’t get cascading delete is that your relationship is optional. Example: public class Category { public int CategoryId { get; set; } } public class Product { public int ProductId { get; set; } public Category Category { get; set; } } In this model you would get a Product table … Read more

Navigation Property without Declaring Foreign Key

I believe, it is not possible to define the relationship only with data attributes. The problem is that EF’s mapping conventions assume that Creator and Modifier are the two ends of one and the same relationship but cannot determine what the principal and what the dependent of this association is. As far as I can … Read more

Entity Framework 4.1 InverseProperty Attribute and ForeignKey

It is theoretically correct but SQL server (not Entity framework) doesn’t like it because your model allows single employee to be a member of both First and Second team. If the Team is deleted this will cause multiple delete paths to the same Employee entity. This cannot be used together with cascade deletes which are … Read more

How to update not every fields of an object using Entity Framework and EntityState.Modified

Let’s assume that you have a collection of the properties to be excluded: var excluded = new[] { “property1”, “property2” }; With EF5 on .NET 4.5 you can do this: var entry = context.Entry(obj); entry.State = EntityState.Modified; foreach (var name in excluded) { entry.Property(name).IsModified = false; } This uses a new feature of EF5 on … Read more

Composite Key with EF 4.1 Code First

You can mark both ActivityID and ActivityName properties with Key annotation or you can use fluent API as described by @taylonr. Edit: This should work – composite key defined with annotations requires explicit column order: public class ActivityType { [Key, Column(Order = 0)] public int ActivityID { get; set; } [Key, Column(Order = 1)] [Required(ErrorMessage … Read more

tech