Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

 

The error message arises if you want to extend an application with EF Entity Framework on Asp.net Core 2 MVC.

As soon as you start the migration, the Feher comes:

No DbContext was found in assembly

 

Error message in the Package Manager Console

PM> add-migration 01Start

Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]

      User profile is available. Using 'C:\Users\Raimund\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.

No DbContext was found in assembly 'ArtikelWeb'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

 

 

Solution:

You have to create a DbContext class under Models that represents the tables to the outer SQL Server database.

In Entity Framework, the database is called dbContext and dbSet is the tables.

The text myWebApp_ must be adapted to the name of your own application

    public class DbContext_Model

    {

 

        public class MyWebApp_Context : DbContext

        {

            public myWebApp_Context(DbContextOptions< myWebApp_Context> options)

                : base(options)

            { }

 

            public DbSet<Products > Products { get; set; }           

        }

    }

 

For this you have to insert the line EntityFrameworkCore at Asp.Net Core

using Microsoft.EntityFrameworkCore;

 

In the directory / Models

 

 

Then add the database connection to the Startup.cs file

 

 

Entry of the connection in the section ConfigureServices.

The name myWebApp must be adapted to the name of your own application

public void ConfigureServices(IServiceCollection services)

        {

            services.AddMvc();

            var connection = @"Server=(localdb)\mssqllocaldb;Database=myWebApp;Trusted_Connection=True;";

            services.AddDbContext<myWebApp_Context>(options => options.UseSqlServer(connection));

        }

 

And in the Import section of Startup.cs, you need to add a reference to the .Models directory and a reference to DbContrext file

using test_EF_Core2.Models;

using Microsoft.EntityFrameworkCore;

using static test_EF_Core2.Models.DbContext_Model;

 

Before you can migrate the application with EF Entity Framework, you still have to set a Primary Key in the Model classes / Tables.

Here you put the attibut line [Key] in front of the field entry, which represents the ID column.

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Linq;

using System.Threading.Tasks;

 

namespace test_EF_Core2.Models

{

    public class Products

    {

        [Key]

        public int IDProduct { get; set; }

        public string Title { get; set; }

    }

}

 

 

Migration and transfer from SQL Server

# Code First

First step ... create the migration

PM> Add-Migration InitialCreate

 

This creates a directory / migration and adds the current migration file to Date_Name.cs

 

And then you transfer the structure to SQL Server:

PM> Update-Database

 

 

Database in SQL Server

 

The SQL Server Object Explorer is preinstalled in Visual Studio and is located in Menu-> View-> SQL Server Object Explorer.

If you then on SQL Server -> (localdb) -> Databases lists the databases, then you will find the same name database in the directory.

 

It is important that in each case a primary key has been adopted in the tables.

 

The statement PM> Update Database thus creates the correct SQL Server Transscript files on the SQL Server.

With the matching T-SQL script.

CREATE TABLE [dbo].[Products] (

    [IDProduct] INT            IDENTITY (1, 1) NOT NULL,

    [Title]     NVARCHAR (MAX) NULL,

    CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ([IDProduct] ASC)

);

 

 

 

This is so important that when entering new data sets, the ID is incremented uniquely and thus a fast and clear structure is available.

 

Video tutorial on the solution

[Lnk "https://www.youtube.com/watch?v=Ng_8uMdsqps" /]

 

Mobile
»
Installation of .NET Core 2.1 on a web server
»
Set upload of very large files in Asp.Net MVC Core 2.
»
Error: Identity column can not be updated, IDENTITY_INSERT is set to OFF
»
Asp Error: IDENTITY_INSERT OFF when accessing AspNetUsers registry
»
ASP IIS Error: 440.13 Not Found. upload file size is exceeded
»
InvalidOperationException: An exception occurred while reading a database value for property
»
A database operation failed while processing the request. SqlException: Invalid object name
»
InvalidOperationException: No database provider has been configured for this dbContext
»
NullReferenceException: Object reference not set to an instance of an object.
»
InvalidOperationException: The expected type was System.Boolean but the actual value was null.

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de