What effect do the different EF 4 SaveOptions have on the ObjectContext?

Short correction for SaveOptions.DetectChangesBeforeSave : this is the default. When you do ObjectContext.SaveChanges(), the method DetectChanges() is called to synchronized attach entities in the OSM. SaveChanges is an overloaded version of SaveChanges(SaveOptions optsions) method, where this parameterless version calls this SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave) SaveOptions is a Flag enum and in conclusion, SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave is … Read more

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

Multiple SaveChanges calls in entity framework

I know it’s kind of late answer but i found it useful to share. Now in EF6 it’s easier to achieve this by using dbContext.Database.BeginTransaction() like this : using (var context = new BloggingContext()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { // do your changes context.SaveChanges(); // do another changes context.SaveChanges(); dbContextTransaction.Commit(); } … Read more

using Guid as PK with EF4 Code First

Have you set Identity StoreGeneratedPattern? You can do it in the OnModelCreating method: modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); or using the DataAnnotation attributes: public class Foo { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id {get;set;} public string Name {get;set;} }

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

Is it possible to change the location of the EF Migrations “Migrations” folder?

In the configuration class constructor add this line: this.MigrationsDirectory = “DirOne\\DirTwo”; The namespace will continue to be set as the namespace of the configuration class itself. To change this add this line (also in the configuration constructor): this.MigrationsNamespace = “MyApp.DirOne.DirTwo”;

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)