EF can’t infer return schema from Stored Procedure selecting from a #temp table

CREATE PROCEDURE [MySPROC] AS BEGIN –supplying a data contract IF 1 = 2 BEGIN SELECT cast(null as bigint) as MyPrimaryKey, cast(null as int) as OtherColumn WHERE 1 = 2 END CREATE TABLE #tempSubset( [MyPrimaryKey] [bigint] NOT NULL, [OtherColumn] [int] NOT NULL) INSERT INTO #tempSubset (MyPrimaryKey, OtherColumn) SELECT SomePrimaryKey, SomeColumn FROM SomeHugeTable WHERE LimitingCondition = true … Read more

Entity Framework Code First – Advantages and disadvantages of Fluent Api vs Data Annotations [closed]

Everything what you can configure with DataAnnotations is also possible with the Fluent API. The reverse is not true. So, from the viewpoint of configuration options and flexibility the Fluent API is “better”. Configuration examples (for sure not a full list) which are possible in the Fluent API but not with DataAnnotations (as far as … Read more

MVC3 and Code First Migrations – “model backing the ‘blah’ context has changed since the database was created”

From my experience that suggests that migration table is out of sync (even if your data isn’t), and that’s been part of the db schema now (since 4.3 I think – under system tables). There could be many reasons and ways to experience that error , but most of the time… The problematic part is … Read more

mapping private property entity framework code first [duplicate]

Here’s a convention you can use in EF 6+ to map selected non-public properties (just add the [Column] attribute to a property). In your case, you’d change TypeId to: [Column] private int TypeId { get; set; } In your DbContext.OnModelCreating, you’ll need to register the convention: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Add(new NonPublicColumnAttributeConvention()); } … Read more

Loop/reflect through all properties in all EF Models to set Column Type

In EF Core v1.1.0 you can use something like this: foreach (var pb in modelBuilder.Model .GetEntityTypes() .SelectMany(t => t.GetProperties()) .Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)) .Select(p => modelBuilder.Entity(p.DeclaringEntityType.ClrType).Property(p.Name))) { pb.ForSqlServerHasColumnType(“decimal(13,4)”); } Update (EF Core 2.x): Starting from EF Core 2.0, the model is built separately for each database provider, so HasAbcXyz methods … Read more

SQL Server Express connection string for Entity Framework Code First

The problem with your connection string here is: <add name=”TrempimModel” connectionString=”data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.sdf; User Instance=true” providerName=”System.Data.SqlClient” /> You’re basically defining what “server” you’re connecting to – but you’re not saying what database inside the file to connect to. Also – the file extension for SQL Server Express database files is .mdf (not .sdf – … Read more

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

If you load the entity from the context you cannot attach an entity with the same key again. The first entity is still kept in internal context cache and context can hold only one instance with given key value per type (it is called identity map and I described it here in other situation). You … Read more