Entity Framework: Set Delete Rule with CodeFirst

What you are looking for can be achieved by setting up an optional association between Guest and Language entities: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Guest>() .HasOptional(p => p.PreferredLanguage) .WithMany() .HasForeignKey(p => p.LanguageID); } Unit Test: using (var context = new Context()) { var language = new Language() { LanguageName = “en” }; var guest … Read more

Entity Framework 4: Does it make sense to create a single diagram for all entities?

Having one big EDM containing all the entities generally is NOT a good practice and is not recommended. Using one large EDM will cause several issues such as: Performance Issue in Metadata Load Times: As the size of the schema files increase, the time it takes to parse and create an in-memory model for this … Read more

How to convert DbSet in Entity framework to ObjectQuery

I found the answer. Of course, it is possible to convert DbSet in Entity framework to ObjectQuery using the below lines of code. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>(“Requests”); where, db – Context class inherting from DbContext. Requests – DbSet<Request> defined in Context class. objectSet – Can now be passed as ObjectQuery.

Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

You can use DateCreated = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”) Usage: public partial class MyMigration : DbMigration { public override void Up() { CreateTable(“dbo.Users”, c => new { Created = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”), }) .PrimaryKey(t => t.ID); … Update 2012-10-10: As requested by Thiago in his comment, I add a little extra context. The code … Read more

Entity Framework Simple Generic GetByID but has differents PK Name

Here is example of base repository class for repositories build for entity with single property key. The GetByKey method is independent on key name or type. using System; using System.Data; using System.Data.Metadata.Edm; using System.Data.Objects; using System.Linq; namespace EntityKeyTest { // Base repository class for entity with simple key public abstract class RepositoryBase<TEntity, TKey> where TEntity … Read more

The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

You cannot use properties that are not mapped to a database column in a Where expression. You must build the expression based on mapped properties, like: var date = DateTime.Now.AddYears(-from); result = result.Where(p => date >= p.DOB); // you don’t need `AsQueryable()` here because result is an `IQueryable` anyway As a replacement for your not … 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