Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data type 'varchar' is not supported in this form #4759

Closed
mikes-gh opened this issue Mar 10, 2016 · 7 comments
Closed

Data type 'varchar' is not supported in this form #4759

mikes-gh opened this issue Mar 10, 2016 · 7 comments

Comments

@mikes-gh
Copy link
Contributor

Just changed to the new AspNetCore packages on vnext with vs 2015 update2 rc
The below used to work before this. I was on the last known good packages before the name change on vnext.

I have a derived DbContext which sets a database depending on a routing value as below

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;

namespace Reports2.Models
{
    public class TradingCompanyDbContext:ReportingDbContext
    {
        private readonly IConfiguration _config;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public TradingCompanyDbContext(IConfiguration config, IHttpContextAccessor httpContextAccessor,DbContextOptions options):base(options)
        {
            _config = config;
            _httpContextAccessor = httpContextAccessor;            
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var tradingCompany = _httpContextAccessor.HttpContext.GetRouteValue("tradingCompany");
            if (tradingCompany != null)
            {
                var connectionString = _config["Data:" + tradingCompany + ":ConnectionString"];
                optionsBuilder.UseSqlServer(connectionString);
            }
        }
    }
}

It seems whenever I use this context I am getting

NotSupportedException: Data type 'varchar' is not supported in this form. Either specify the length explicitly in the type name, for example as 'varchar(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type

Any ideas.
This is a preliminary inquiry . I realise a repro would be best but maybe its a known thing before I go down that road

@ajcvickers
Copy link
Member

Are you specifying the store type "varchar" in a data annotation or in OnModelCreating? If so, change it as suggested in the message. If not, then please post your model classes and any code you have in OnModelCreating.

See this PR : #4500, and also this open issue: #3420

@mikes-gh
Copy link
Contributor Author

Ok the on model creating was scafolded a while back by reveng . I see hascolumntype is varchar (note no length) and hasmaxlength as length. I presume this has changed now? Shall I re reveng or is there a new dotnet cli tool now.

@lajones
Copy link
Contributor

lajones commented Mar 10, 2016

The RevEng tool has been updated to produce the right results - see #4348. Or you can just change the code manually to specify e.g. HasColumnType("varchar(<length>)").

@mikes-gh
Copy link
Contributor Author

Ok thanks it's a lot of fields so I'll reveng again. I'll do it tomorrow and close this if it all goes ok.

@mikes-gh
Copy link
Contributor Author

OK I had to manually change (long job) this because I couldn't get donet ef to work or dnx ef.

Probably due to the flux at the moment.
Im going to open another issue for that

Thanks for your help

@mikes-gh mikes-gh changed the title Failing query on derived DbContext Data type 'varchar' is not supported in this form Mar 11, 2016
@lajones
Copy link
Contributor

lajones commented Mar 11, 2016

Sorry - it is a problem at the moment (hopefully fixed soon). But I should have said that the Scaffold-DbContext powershell command within Visual Studio has been working consistently even when the command line dotnet ef command does not. If you run into trouble again that might help you.

@MalithRukshan
Copy link

MalithRukshan commented Mar 22, 2018

I also got the same error: =>{Data type 'varchar' is not supported in this form. Either specify the length explicitly in the type name, for example as 'varchar(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type.'}

before fixed :
.HasColumnType("varchar")
.HasMaxLength(50);
solution is:
.HasColumnType("varchar(50)");

It is working.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants