Unique Key constraints for multiple columns in Entity Framework

With Entity Framework 6.1, you can now do this: [Index(“IX_FirstAndSecond”, 1, IsUnique = true)] public int FirstColumn { get; set; } [Index(“IX_FirstAndSecond”, 2, IsUnique = true)] public int SecondColumn { get; set; } The second parameter in the attribute is where you can specify the order of the columns in the index. More information: MSDN

Ignoring a class property in Entity Framework 4.1 Code First

You can use the NotMapped attribute data annotation to instruct Code-First to exclude a particular property public class Customer { public int CustomerID { set; get; } public string FirstName { set; get; } public string LastName{ set; get; } [NotMapped] public int Age { set; get; } } [NotMapped] attribute is included in the … Read more

Entity Framework Stored Procedure Table Value Parameter

UPDATE I’ve added support for this on Nuget Package – https://github.com/Fodsuk/EntityFrameworkExtras#nuget (EF4,EF5,EF6) Check out the GitHub repository for code examples. Slightly off question, but none the less useful for people trying to pass user-defined tables into a stored procedure. After playing around with Nick’s example and other Stackoverflow posts, I came up with this: class … Read more

EntityType ‘IdentityUserLogin’ has no key defined. Define the key for this EntityType

In my case I had inherited from the IdentityDbContext correctly (with my own custom types and key defined) but had inadvertantly removed the call to the base class’s OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // I had removed this /// Rest of on model creating here. } Which then fixed up my missing … Read more

What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?

So far, I know of these effects. Lazy Loading: Any virtual ICollections will be lazy-loaded unless you specifically mark them otherwise. More efficient change tracking. If you meet all the following requirements then your change tracking can use a more efficient method by hooking your virtual properties. From the link: To get change tracking proxies, … Read more

Unique key with EF code first

Unfortunately you can’t define it as unique key in code first because EF doesn’t support unique keys at all (it is hopefully planned for next major release). What you can do is to create custom database intializer and add unique index manually by calling SQL command: public class MyInitializer : CreateDatabaseIfNotExists<MyContext> { protected override void … Read more

Decimal precision and scale in EF Code First

The answer from Dave Van den Eynde is now out of date. There are 2 important changes, from EF 4.1 onwards the ModelBuilder class is now DbModelBuilder and there is now a DecimalPropertyConfiguration.HasPrecision Method which has a signature of: public DecimalPropertyConfiguration HasPrecision( byte precision, byte scale ) where precision is the total number of digits … Read more

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea? [closed]

You can have multiple contexts for single database. It can be useful for example if your database contains multiple database schemas and you want to handle each of them as separate self contained area. The problem is when you want to use code first to create your database – only single context in your application … Read more

EF codefirst : Should I initialize navigation properties?

Collections: It doesn’t matter. There is a distinct difference between collections and references as navigation properties. A reference is an entity. A collections contains entities. This means that initializing a collection is meaningless in terms of business logic: it does not define an association between entities. Setting a reference does. So it’s purely a matter … Read more