Consolidating EF migrations into new InitialCreate

Consider reading this nice article from Rick Strahl : https://weblog.west-wind.com/posts/2016/jan/13/resetting-entity-framework-migrations-to-a-clean-slate Basically the solution is not trivial and needs more than just reseting all the migrations into one because you have two scenarios that needs to fit in ONE migration class: Create a new database => the migration class should contain every table creation My database … Read more

Is it possible to set a unique constraint using Entity Framework Code First?

It appears that the unique constraint feature that was scheduled to release with Version 6 got pushed to 6.1. With EF 6.1, you can define a constraint using the Index attribute as shown below: [Index(“IX_FirstAndSecond”, 1, IsUnique = true)] public int FirstColumn { get; set; } [Index(“IX_FirstAndSecond”, 2, IsUnique = true)] public int SecondColumn { … Read more

Get return value from stored procedure

I found it! I can read the return value with an output parameter that has to be used in this way: // define a new output parameter var returnCode = new SqlParameter(); returnCode.ParameterName = “@ReturnCode”; returnCode.SqlDbType = SqlDbType.Int; returnCode.Direction = ParameterDirection.Output; // assign the return code to the new output parameter and pass it to … Read more

Non-static method requires a target. Entity Framework 5 Code First

The problem boiled down to the query. My original question had this query: var allPartners = DbContext.User .Include(u => u.Businesses) .Where(u => u.Businesses.Any(x => x.Id == currentBusinessId)) .ToList(); Which wasn’t quite accurate, I had in fact removed the error in an attempt to ask my question succinctly. The query was actually: var currentBusiness = GetBusiness(); … Read more

DbContext AutoDetectChangesEnabled set to false detecting changes

Setting AutoDetectChangesEnabled to false doesn’t disable change tracking. (That’s what the AsNoTracking() extension method would do.) It just disables the automatic call of DetectChanges that would otherwise occur in many DbContext API methods. But DetectChanges isn’t the only method that participates in change tracking. However, if you don’t call it manually at the right places … 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

Improve navigation property names when reverse engineering a database

There a few things you need to change inside the .tt file. I choose to use the third solution you suggested but this requires to be formatted like FK_CollectionName_RelationName. I split them up with ‘_’ and use the last string in the array. I use the RelationName with the ToEndMember property to create a property … 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

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