locked
Deploy with EntityFramework RRS feed

  • Question

  • User242218449 posted

    Hi

    I was able to successfully follow the tutorial about razor pages (https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/ ).

    As a second step I would like to deploy the app to my web server (IIS). I managed to get the app up and running on the server as long as there are no database actions involved. An exception is thrown if I want to access the database. The exception I get is:

    System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Movie'. bei System.Data.SqlClient.SqlCommand.

    I did create a database but did not create any tables or the like. On the local system I used to do this by PMC commands like Add-Migration / Update-Database.
    How is this supposed to work on the web server? Should EF take care of creating tables etc. or do I have to manually create this and also modify it manually as we go?

    Sorry, it may sound like a stupid question but I wasn't able to find an answer myself.

    Thanks for your help

    Tuesday, November 14, 2017 6:15 PM

All replies

  • User1120430333 posted

    How is this supposed to work on the web server? Should EF take care of creating tables etc. or do I have to manually create this and also modify it manually as we go?

    No IMO, EF should not be creating databases and tables, although you can do so.

    IMO, you should have some kind of basic Database Administration skills where you create the DB and all the tables in the DB constrains and all by using a DB administration tool, like MS SQL Server Management Studio. You can the use scripting T-SQL, which SSMS would make for you  scripts from a local database, as an example.  You can then take the scripts and run them through SSMS and deploy to any DB server you wanted to. This is what DBA(s) do.

     How is this supposed to work on the web server?

    A Web server is separate from a DB server where as you deploy the executables for the Web application, you set the connectionstring pointing to the DB server and the DB and that's it.

    You run the DB scripts at the database first before application is deployed to IIS

    I am from the old school,  and we as developers were taught basic DB administration skills by using teaching material. Even to this day, I would never let an ORM like EF do DB administration.

     

    Wednesday, November 15, 2017 12:19 AM
  • User-335504541 posted

    Hi nex23473,

    nex23473

    How is this supposed to work on the web server? Should EF take care of creating tables etc. or do I have to manually create this and also modify it manually as we go?

    You should manually create the data tables.

    But you could also use PMC commands.

    EF can find the database by the connection string.

    If you change the connection string in the local system to the one for the web server database, you could manipulate the database like you do on the local system.

    Best Regards,

    Billy

    Wednesday, November 15, 2017 7:28 AM
  • User242218449 posted

    Thanks to both of you. I have created the table and now everything seems to be fine.

    However, I still do not get why EF was not able to create it automatically. The PMC command Add-Migration did create a migration file with the following content:

        public partial class Initial : Migration
        {
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.CreateTable(
                    name: "Movie",
                    columns: table => new
                    {
                        ID = table.Column<int>(type: "int", nullable: false)
                            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                        Genre = table.Column<string>(type: "nvarchar(max)", nullable: true),
                        Price = table.Column<decimal>(type: "decimal(18, 2)", nullable: false),
                        ReleaseDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                        Title = table.Column<string>(type: "nvarchar(max)", nullable: true)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_Movie", x => x.ID);
                    });
            }
    
            protected override void Down(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.DropTable(
                    name: "Movie");
            }
        }

    I mean I totally agree with you both and I also used to create and modify data tables manually in the past but why having these code lines? My expectation was to not have to perform any manual database manipulations anymore.

    For an initial database setup it isn't a major issue as it is a one time job. Extending data tables during the lifecycle of an application is something which may happen with every new release. For me it would be very beneficial if this could be automated (after successfull tests on a test server).

    Do you know some good documentation about this topic? I somehow think I'm missing a major point in the concept.

    Wednesday, November 15, 2017 6:32 PM
  • User-335504541 posted

    Hi nex23473,

    Do you know some good documentation about this topic?

    I think you could refer to the following tutorial:

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application#install-entity-framework-6

    Best Regards,

    Billy

    Thursday, November 16, 2017 1:59 AM