1

I've the same problem described here and I've already try to apply the suggested solution changing the connection string name to the FQN of my db context which is placed in another project.

Web project Web.config file (in project MyApplication.Web):

<connectionStrings>
<add name="FarmaciaNataliniServer.Infrastructure.ApplicationDbContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-FarmaciaNataliniServer-20151127115838.mdf;Initial Catalog=aspnet-FarmaciaNataliniServer-20151127115838;Integrated Security=True" providerName="System.Data.SqlClient" />

ApplicationDbContext.cs (in project MyApplication):

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<RegisteredDevice> RegisteredDevices { get; set; }
    public DbSet<Reservation> Reservations { get; set; }

    public ApplicationDbContext() : base("FarmaciaNataliniServer.Infrastructure.ApplicationDbContext", throwIfV1Schema: false)
    {
    }

...

Unfortunately seems that this solution does not work anymore on Visual Studio 2015 :(

Can anyone help me with that?

Thank you all in advance!

0

2 Answers 2

1

I found the solution to my problem. I've createad a new fresh project adding files and dependencies one by one and I found which ones that make the option disappear.

They were:

  • Autofac.Owin
  • Autofac.Mvc.Owin
  • Autofac.WebApi2.Owin

and a Controller that inherits from KnockoutController from package PerpetuumSoft.Knockout.

I've removed these dependendecies and now the Code-First option on publish dialog is visible and activable again.

Don't know why these dependencies cause that problem, I don't see any correlation but now it's working as desired. :)

Sign up to request clarification or add additional context in comments.

Comments

0

The EF code first tooling (> EF 6.X ) works very well and supports your use-case out-of-the-box to allow developers to follow best-practices by separating DB/EF from Web context/projects. It is really a matter of understanding the tools and of doing things in the right way. I would recommend you setup a fresh solution to just try code-first migrations. If you do not get this working in an isolated way, do not throw away your time while testing over and over again in your real project/solution.

While settings up code-first migrations in our project my team was supported by Microsoft employees. They just did it right and we saved some bits in our documentation.

While browsing the Internet I found these pages helpful, I know there is plenty of help to deal with EF code-first development and deployment (especially using Visual studio deployment to Azure App service).

Since your question is rather difficult to answer, I would recommend you follow best-practices and documentation written for EF 6.X and VS2015 and come back to SO with a better understanding of tools you need to solve your problem. Then either edit your question or just ask another ones.

To give you some appetizers just look at the help of migrate.exe - a tool to run code-first migration from commandline. There is everything you probably need.

migrate.exe

Code First Migrations Command Line Utility
Applies any pending migrations to the database.

migrate assembly  [configurationType]  [contextAssembly]  [/targetMigration]
        [/startUpDirectory]  [/startUpConfigurationFile]
        [/startUpDataDirectory]  [/connectionStringName]
        [/connectionString]  [/connectionProviderName]  [/force]  [/verbose]
        [/?]

assembly                     Specifies the name of the assembly that
                             contains the migrations configuration type.
[configurationType]          Specifies the name of the migrations
                             configuration type. If omitted, Code First
                             Migrations will attempt to locate a single
                             migrations configuration type in the specified
                             assembly.
[contextAssembly]            Specifies the name of the assembly that
                             contains the DbContext type if different from
                             the assembly that contains the migrations
                             configuration type.
[/?]                         Display this help message.
[/connectionProviderName]    Specifies the provider invariant name of the
                             connection string.
[/connectionString]          Specifies the connection string to use. If
                             omitted, the context's default connection will
                             be used.
[/connectionStringName]      Specifies the name of the connection string to
                             use from the specified configuration file. If
                             omitted, the context's default connection will
                             be used.
[/force]                     Indicates that automatic migrations which might
                             incur data loss should be allowed.
[/startUpConfigurationFile]  Specifies the Web.config or App.config file of
                             your application.
[/startUpDataDirectory]      Specifies the directory to use when resolving
                             connection strings containing the
                             |DataDirectory| substitution string.
[/startUpDirectory]          Specifies the working directory of your
                             application.
[/targetMigration]           Specifies the name of a particular migration to
                             update the database to. If omitted, the current
                             model will be used.
[/verbose]                   Indicates that the executing SQL and additional
                             diagnostic information should be output to the console window

According what configuration parameters you pass to the migration tooling you either run an explicit, a configuration-file based or a convention-based migration. Pay close attention to what migrate is printing as output of Origin

1. Explicit parameters

migrate.exe "Fireframework.Web.dll" "Fireframework.Web.Migrations.Configuration" /connectionString="Data Source=tcp:fireframeworkdbs.database.windows.net,1433;Initial Catalog=fireframeworkdbdev;User Id=ffdbadmin@fireframeworkdbs;Password="secret" /connectionProviderName="System.Data.SqlClient" /verbose

2016-04-25T08:06:09.9183260Z VERBOSE: Target database is: 'fireframeworkdbdev' (DataSource: tcp:fireframeworkdbs.database.windows.net,1433, Provider: System.Data.SqlClient, **Origin: Explicit**).

2016-04-25T08:06:13.5640428Z No pending explicit migrations.

2. By Configuration file

migrate.exe "Fireframework.Web.dll" "Fireframework.Web.Migrations.Configuration" /startUpConfigurationFile="web.config" /verbose

VERBOSE: Target database is: 'fireframework' (DataSource: (LocalDb)\MSSQLLocalDB, Provider: System.Data.SqlClient, **Origin: Configuration**).
Applying explicit migrations: [201603161334138_InitialCreate, 201604081002396_Add_Simulation_CreatedDateTime_Property, 201604201138442_Add_Simulation_AbortRequestPending_Property, 201604211315107_Rename_Simulation_Properties].
Applying explicit migration: 201603161334138_InitialCreate.

3. By Convention

migrate.exe "Fireframework.Web.dll" "Fireframework.Web.Migrations.Configuration" 

VERBOSE: Target database is: 'Fireframework.Model.DatabaseContext' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, **Origin: Convention**).

4 Comments

Thank you for you exaustive exaustive answer but I've already done all that you advise me to do. The problem is that Code first migration option is missing because visual studio does not recognize my context as of CodeFirst type and for now i didn't find a solution.
please add relevant parts like <connectionStrings> and <entityFramework> of your web.config to your question to allow people to debug your specific problem. These parts are shown at the end of ContosoUniversity EF code-first tutorial as well.
The structure is a bit articulated and the proble may be everything (I think) so I prefer to share you the link to my gdrive shared folder which contains the project: drive.google.com/open?id=0B-vx6bPPHKprVHdaVURPbkdUZXM. It seems some depedendecy is causing the problem but I've not find out which. Please, if you can take a look to my project.
I found the solution and solved the problem, for those interested I've edited the first post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.