'TypeConverter' does not contain a definition for 'IsValid' in MVC6 - visual-studio-2005

i am using MVC 6 and i have referred the following name space,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Syncfusion.JavaScript.Shared.Serializer;
using System.Reflection;
here is my code,
static bool CanConvertTo<T>(string s) {
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
return converter.IsValid(s);
}
it throw the error like this,
'TypeConverter' does not contain a definition for 'IsValid' and no extension method 'IsValid' accepting a first argument of type 'TypeConverter' could be found (are you missing a using directive or an assembly reference?)
this was reproduced only in MVC6
how to resolve this and there is any alternate solution for this?

There is IsValid method in System, Version=4.0.0.0:
namespace System.ComponentModel
{
public class TypeConverter
{
// other members
public bool IsValid(object value)
{
// some implementation
}
}
}
There are two frameworks in your configuration: dnx451 and dnxcore50. I'm not sure the method IsValid exists in dnxcore50.
If you use only dnx451 framework, I recommend you try it:
1) Switch the project framework to dnx451 (or delete dnxcore50 section from project.json). If it's useless then:
2) Update the project to MVC 6 beta 7 and use next configuration in project.json:
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-beta7",
"EntityFramework.Commands": "7.0.0-beta7",
"Microsoft.AspNet.Mvc": "6.0.0-beta7",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta7",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.Facebook": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.Google": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.MicrosoftAccount": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.Twitter": "1.0.0-beta7",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta7",
"Microsoft.AspNet.Diagnostics.Entity": "7.0.0-beta7",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta7",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta7",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta7",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta7",
"Microsoft.Framework.Configuration.Abstractions": "1.0.0-beta7",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7",
"Microsoft.Framework.Configuration.UserSecrets": "1.0.0-beta7",
"Microsoft.Framework.Logging": "1.0.0-beta7",
"Microsoft.Framework.Logging.Console": "1.0.0-beta7",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta7"
},
"frameworks": {
"dnx451": { }
}
I tried your example code in a project with this configuration and there are not any problems. My solution DNX SDK version is 1.0.0-beta7.

Related

use Laravel Excel inside laravel package development

I'm creating my own Laravel package for the first time. I create a new project and require orchestra/testbench in the project. Things look okay and I'm able to run tests inside the package but I couldn't use Laravel Excel inside my package.
in composer.json I added
"extra": {
"laravel": {
"providers": [
"Maatwebsite\\Excel\\ExcelServiceProvider"
],
"aliases": {
"Excel": "Maatwebsite\\Excel\\Facades\\Excel",
}
}
},
"require-dev": {
"orchestra/testbench": "6.0",
"phpunit/phpunit": "^9.5",
"maatwebsite/excel": "^3.1"
}
And also ran composer dump-autoload, when I want to use Laravel Excel inside my package I tried
use Maatwebsite\Excel\Facades\Excel;
class TaxCalculation {
public function incomeTax(): float
{
$table = Excel::import(new TaxImport(), 'file.xls');
}
But got an error
Illuminate\Contracts\Container\BindingResolutionException: Target class [excel] does not exist.
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:712
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:796
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:651
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:781
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:1354
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:198
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:166
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:255
D:\code\packages\thai_tax\src\Services\TaxCalculation.php:45
D:\code\packages\thai_tax\tests\Unit\CreateFacadeTest.php:26
Caused by
ReflectionException: Class excel does not exist
Excel does not seem to load. Is there any more steps I need to do to use it?

An error occurred applying migrations, try applying them from the command line

I am on visual studio 2019 for mac running a blazor server app with .net core 3.1 and Individual Authentication (In-app) turned on.
When i go to register and enter new users details i am presented with the following error when clicking the apply migrations button
In the appsettings.json i have the following set.
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=Test; user=SA; password=P#55word; Trusted_Connection=False;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CMUI.Areas.Identity;
using CMUI.Data;
namespace CMUI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
The the SQL server i am running is 2019 mssql in docker using the following command
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=P#55word' -p 1433:1433 -d --name=mssqlserver2019 mcr.microsoft.com/mssql/server:2019-latest
The database is working okay as i can perform crud actions via an webapi in another solution using the same connection string. Not sure if this is a mac thing or if i have missed something silly.
Thanks.
You can try using the command line and navigating to the project root of the project that connects to that db, and then running dotnet ef database update which should run that migration and build your identity tables. Then fire the app up again and as long as it's connecting (which is looks like you are) you should be able to register users.
Further reading on migrations here. You may need to install the command line tools mentioned in this article.
I'm not familiar with VS for MacOS, but in the windows version you can go to Package Manager Console, make sure the default project in the console is set to your DB access project, and then run the command update-database. This might work for you as well.

Entity Framework Core Migration for ASP.Net Core Class Library

I've been trying to follow the advice of Ben Cull (http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects), but the database is a little different in that I'm trying to inherit from ASP.NET Core IdentityUser class. I created a new solution containing the default ASP.NET Core Web Application from the VS2015 template (CodeFirstTest). I then added an ASP.NET Core class-library (CodeFirstTest.User) to the solution, which would be the data layer in the application and where I will also be setting up ASP.NET Core Identity.
Following the advice of Ben Cull, I rewrote the CodeFirstTest.User project.json as follows.
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.Extensions.Configuration": "1.0.1",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
I also created a Program.cs file containing the entry point, and a User class that inherits from ASP.NET Core IdentityUser as shown below.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace CodeFirstTest.User
{
public class Program
{
public static void Main(string[] args) { }
}
public class User : IdentityUser
{
}
public class UserIdentityDbContext : IdentityDbContext<User>
{
public UserIdentityDbContext(DbContextOptions options)
{
}
}
public class TemporaryDbContextFactory : IDbContextFactory<UserIdentityDbContext>
{
public UserIdentityDbContext Create(DbContextFactoryOptions options)
{
var builder = new DbContextOptionsBuilder<UserIdentityDbContext>();
builder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=UserDb;Trusted_Connection=SqlTruncateException;MultipleActiveResultSets=true");
return new UserIdentityDbContext(builder.Options);
}
}
}
When I use the Project Manager Console to create the initial migration, I received the following errors.
PM> Add-Migration -Name "Initial" -Project "CodeFirstTest.User"
Could not invoke this command with the startup project 'CodeFirstTest'. Check that 'Microsoft.EntityFrameworkCore.Design' has been added to "dependencies" in the startup project and that the version of 'Microsoft.EntityFrameworkCore.Tools' in "tools" and 'Microsoft.EntityFrameworkCore.Design' are the same. See http://go.microsoft.com/fwlink/?LinkId=798221 for more details.
PM> Add-Migration -Name "Initial" -Project "CodeFirstTest.User"
Unhandled Exception: System.MissingMethodException: Entry point not found in assembly 'Microsoft.EntityFrameworkCore.Design, Version=1.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
I corrected the first error, but the second run will cause dotnet to fail with the 'Unhandled Exception' error.
The question...
Am I coding something incorrectly that prevents the migration from executing?
Thank you.
UPDATE: And now this answer appears to be out of date! Turns out 1.1 has removed this feature. After upgrade to 1.1 it no longer works. Odd that this would stop working. Setting up the class library to work like a console application as Ben Cull suggests appears to be the way to handle it when using EF Core 1.1
That blog article is pretty old. Most things that are from before the .Net Core release while still usually useful, need to be taken with a grain of salt.
You no longer have to fake out a console app in the class library. I whipped together a sample identity application where the User and DbContext were in a class library.
The class library project.json looked like this:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
To show the ClassLibrary namespace and the identity schema coming through in the migration.
A few subtle things to note:
Passed the options in the DbContext constructor to the base constructor
WebApplication was the startup project. The default project in the Package Manager Console was the Class Library. This is so that it knows where to find Startup.
The whole example solution I put together has been put up on Github if you want to use it as a baseline.
for core 1.1
after adding the packages with nuget it will not work directly, you will need also to edit the .csproj file like that
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
</ItemGroup>
</Project>
reference: https://www.benday.com/2017/02/14/walkthrough-entity-framework-core-1-1-with-migrations/

MVC 6 - Razor Views in Class Library tag "Model" with a does not exist error

Visual Studio (2015) is underlining all my references to "Model" in the Class Library Razor views and giving this error message:
The name "Model" does not exist in the current context
I have seen a lot of questions asking this sort of thing in an earlier version of ASP.Net and MVC but not much for this version.
Since these Razor views are setup as an embedded resource they are compiling fine and running fine. They just give me errors in the editor.
In the project.json file I have a reference to Razor Tools that I thought should fix this:
"dependencies": {
"ApolloWeb.Common": "1.0.0-*",
"Microsoft.AspNet.Mvc": "5.2.3",
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.Extensions.FileProviders.Embedded": "1.0.0",
"NETStandard.Library": "1.6.1-preview1-24530-04"
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net46": {}
},
"buildOptions": {
"embed": [ "_Views/**" ],
"preserveCompilationContext": true
}
I have discovered what my issue was.
I copied all the packages that the MVC template pulled into the Main MVC project and started removing them until I reached the one that reproduced the issue.
I was missing a dependency on this package:
"Microsoft.AspNetCore.Mvc": "1.0.1",

Using PSR-0 in composer.json does not load my classes

In composer.json I have the following data:
{
"require": {
"slim/slim": "2.*",
},
"autoload": {
"psr-0": {
"lib": "lib/"
}
}
}
lib folder resides beside vendor folder. Inside of lib I have a class named Decorator.php and my class name is Decorator as below:
namespace lib;
class Decorator
{
public function OutputDecorate()
{
return true;
}
}
I ran php compooser.phar update and get the message Nothing to install or update blah blah blah.
And to have a better understanding, this is a part of project folder structure:
Now when I want ti instantiate my class I cannot access my class.
use lib;
class Collection {
public $decorator;
public function __construct() {
$this->decorator = new Decorator();
}
You should be doing:
use lib\Decorator;
if you want to be able to do new Decorator(); in your code.
The PHP use statement only includes a single class, it doesn't import whole namespaces.
Are you a recovering Java programmer?
Also for PSR-0, the declaration in the composer.json of where classes are needs to point at containing directory not the top level namespace. e.g. I think it should be:
"psr-0": {
"lib": "./"
}
or
"psr-0": {
"lib": ""
}
I used psr-4 instead and then my classes are loading perfectly.
Here is my composer.json.
{
"autoload": {
"psr-4": {
"ComposerTest\\Models\\": "App/Models",
},
}
}
And the Folder structure is
-->App
-->Models
-->Product.php

Resources