Can EF automatically delete data that is orphaned, where the parent is not deleted?

It is actually supported but only when you use Identifying relation. It works with code first as well. You just need to define complex key for your ChildObject containing both Id and ParentObjectId: modelBuilder.Entity<ChildObject>() .HasKey(c => new {c.Id, c.ParentObjectId}); Because defining such key will remove default convention for auto incremented Id you must redefine it … Read more

Mocking EF DbContext with Moq

I managed to solve it by creating a FakeDbSet<T> class that implements IDbSet<T> public class FakeDbSet<T> : IDbSet<T> where T : class { ObservableCollection<T> _data; IQueryable _query; public FakeDbSet() { _data = new ObservableCollection<T>(); _query = _data.AsQueryable(); } public virtual T Find(params object[] keyValues) { throw new NotImplementedException(“Derive from FakeDbSet<T> and override Find”); } public … Read more

how to create an audit trail with Entity framework 5 and MVC 4

What I recommend you is to use the ChangeTracker property in EF. Inside your DBContext.cs you will have this: public class DBContext : DbContext { public DBContext () : base(“DatabaseName”) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } public DbSet<YourPocoModelNameHere > YourPocoModelNameHere { get; set; } // This is overridden to prevent someone from … Read more

EF migration for changing data type of columns

You have a default constraint on your column. You need to first drop the constraint, then alter your column. public override void Up() { Sql(“ALTER TABLE dbo.Received DROP CONSTRAINT DF_Receiv_FromN__25869641”); AlterColumn(“dbo.Received”, “FromNo”, c => c.String()); AlterColumn(“dbo.Received”, “ToNo”, c => c.String()); AlterColumn(“dbo.Received”, “TicketNo”, c => c.String()); } You will probably have to drop the default constraints … Read more

Looking for a Ninject scope that behaves like InRequestScope

UPDATE: This approach works against NuGet current, but relies in an anomaly in the InCallscope implementation which has been fixed in the current Unstable NuGet packages. I’ll be tweaking this answer in a few days to reflect the best approach after some mulling over. NB the high level way of structuring stuff will stay pretty … Read more

Mapping Database Views to EF 5.0 Code First w/Migrations

You have specified that the ClientStatisticsView entity should be mapped to a table named “ClientStatistics”. So entity framework will generate a migration containing an instruction to create that table. But you have independently created that view in the database so to prevent the error you are getting you should remove the CreateTable instruction from the … Read more

The type initializer for ‘System.Data.Entity.Internal.AppConfig’ threw an exception

Do the following in the App.config file, Put the connectionStrings element is after the configSections element. Put the startup element after the connectionStrings element. <?xml version=”1.0″ encoding=”utf-8″?> <configuration> <configSections> <section name=”entityFramework” type=”System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ /> </configSections> <connectionStrings> <add name=”SchedulingContext” connectionString=”Data Source=XXX\SQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX” providerName=”System.Data.SqlClient”/> </connectionStrings> <startup> <supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5″ /> </startup> … Read more

Calculated column in EF Code First

You can create computed columns in your database tables. In the EF model you just annotate the corresponding properties with the DatabaseGenerated attribute: [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public double Summ { get; private set; } Or with fluent mapping: modelBuilder.Entity<Income>().Property(t => t.Summ) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed) As suggested by Matija Grcic and in a comment, it’s a good idea to make … Read more