Setting unique Constraint with fluent API?

On EF6.2, you can use HasIndex() to add indexes for migration through fluent API. https://github.com/aspnet/EntityFramework6/issues/274 Example modelBuilder .Entity<User>() .HasIndex(u => u.Email) .IsUnique(); On EF6.1 onwards, you can use IndexAnnotation() to add indexes for migration in your fluent API. http://msdn.microsoft.com/en-us/data/jj591617.aspx#PropertyIndex You must add reference to: using System.Data.Entity.Infrastructure.Annotations; Basic Example Here is a simple usage, adding an … Read more

Entity Framework Core 2.0: How to configure abstract base class once

If I understand correctly, the Status is just a base class and not a base entity participating in Database Inheritance. In such case it’s important to never refer to Status class directly inside entity model and configuration, i.e. no DbSet<Status>, no navigation properties of type Status or ICollection<Status>, no modelBuilder.Entity<Status>() calls and no IEntityTypeConfiguration<Status>. Instead, … Read more

One to one optional relationship using Entity Framework Fluent API

EF Code First supports 1:1 and 1:0..1 relationships. The latter is what you are looking for (“one to zero-or-one”). Your attempts at fluent are saying required on both ends in one case and optional on both ends in the other. What you need is optional on one end and required on the other. Here’s an … Read more

How to do a join in linq to sql with method syntax?

var result = from sc in enumerableOfSomeClass join soc in enumerableOfSomeOtherClass on sc.Property1 equals soc.Property2 select new { SomeClass = sc, SomeOtherClass = soc }; Would be equivalent to: var result = enumerableOfSomeClass .Join(enumerableOfSomeOtherClass, sc => sc.Property1, soc => soc.Property2, (sc, soc) => new { SomeClass = sc, SomeOtherClass = soc }); As you can … Read more