How should I set up my integration tests to use a test database with Entity Framework?

Thanks so much to @Justin and @Petro for your answers, which have helped me immensely. The solution I have come up with is a combination of the techniques you suggested. The solution described below provides a new database for each run of the tests, and a separate transaction for each test. I added a connection … Read more

Entity Framework Code First Using One column as Primary Key and another as Auto Increment Column

You can also do this with Data Annotations: public class Sale { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Key] public string TrNo { get; set; } public DateTime Date { get; set; } public int CustomerID { get; set; } public ObservableCollection<SaleDetail> SaleDetails { get; set; } }

How can I prevent EF “The context cannot be used while the model is being created” errors?

I finally figured out the true cause of this, at least for me. The issue was that I was retrieving a DbContext from Windsor in my custom Asp.Net Membership provider. This caused an issue because the membership provider has a lifespan of the whole application, while all other retrieval calls for the db context were … 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

Self referencing / parent-child relationship in Entity Framework

You must define the ParentId in the category class as nullable to use it as the foreign key property for an optional relationship: public int? ParentId { get; set; } An int property cannot take the value null and therefore cannot represent a NULL as value in a database column.

Primary /Foreign Key in Entity Framework

I think by “not using EF ORM designer” you mean new DbContext API from EF 4.1. Because if you don’t mean DbContext API you still have to use EDMX (designer). You can either use data annotations (System.ComponentModel.DataAnnotations): KeyAttribute and ForeignKeyAttribute: public class Registry { public virtual int Id { get; set; } public virtual MyEntity … Read more

Entity Framework Code First : how to annotate a foreign key for a “Default” value?

The problem is that when you have multiple relationships between two entities, EF Code First isn’t able to find out which navigation properties match up, unless, you tell it how, here is the code: public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string ClientName { get; set; } /****Change Nullable<int> … Read more

Entity Framework Code First Lazy Loading

This is wrong “virtual” keyword is used for not loading the entities unless you explicit this (using an “Include” statement) Lazy Loading means that entities will be automatically loaded when you first access collection or navigation property, and that will happen transparently, as though they were always loaded with parent object. Using “include” is loading … Read more

tech