Skip to content

Not sure if it is a bug or I'm doing something wrong: Scaffolding doesn't work for "varchar" data type #21140

New issue

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

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

Already on GitHub? # to your account

Closed
payiAzure opened this issue Jun 4, 2020 · 3 comments · Fixed by #21829

Comments

@payiAzure
Copy link

In my service, trying to use EF Core DBContext for service to access to database. In my Web API controller, I use constructor injection to get the db context, and when debugging, I can see the dbContext was injected into the controller and can be used. But the entity in the DbContext always has exception:
System.ArgumentException: 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.

I did the following config on DbContext for my asp.net core web api in service fabric.

MyWebService.cs, config the dbcontext, and inject it into service. piece of code is like:

.ConfigureServices(services => services.AddDbContext<MyDbContext>(options =>
   {
      options.UseSqlServer("<sql_db_connectionString>", sqlServerOptionsAction: sqlOptions =>
      {
         sqlOptions.EnableRetryOnFailure(
             maxRetryCount: 5,
             maxRetryDelay: 10,
             errorNumbersToAdd: null
         );
      });
   }))

MyDbContext.cs

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {}
    public virtual DbSet<Features> Features { get; set; }
}

The entity Features class, which is mapping to a table in database

[Table("Features")]
public class Features
{
    [Column(TypeName = "VARCHAR")]
    [MaxLength(50)]
    public string FeatureName { get; set; }
    // other properties...
}

And in Web API controller, use constructor injection to get the dbcontext:

public class MyController : ControllerBase
{
    private MyDbContext myDbContext;
    public MyController(MyDbContext dbContext)
    {
        myDbContext = dbContext;
    }

    public string GetData()
    {
      var result = from strings in this.myDbContext.Features .........
    }
}

The above config way( way1 )--- using attribute in Entity gives me the VARCHAR not supported data type exception.

I also tried to use fluent API (way 2) to config entity property, for which I remove the attribute for the entity property like the following, but still same exception.

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {}
    public virtual DbSet<Features> Features { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Features>()
            .Property(e => e.FeatureName)
            .HasColumnName("FeatureName")
            .HasColumnType("varchar(50)");
    }
}

[Table("Features")]
public class Features
{
    public string FeatureName { get; set; }
    // other properties...
}

image

Steps to reproduce

Further technical details

EF Core version: 3.1.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .netcoreapp 3.1
Operating system:
IDE: Visual Studio 2019 16.6.0

@ajcvickers
Copy link
Contributor

Related #15159

@ajcvickers
Copy link
Contributor

Note for team: As far as I can tell, we never made the change to make this work. I can't find anything indicating that we changed our mind saying that we should not do this. To me, this still seems liek a useful feature to support.

@MaxG117
Copy link

MaxG117 commented Jun 10, 2020

While working on our database provider I also discovered that MaxLength is ignored when TypeName is specified. The source code expects TypeName to be correct and complete, and "varchar" without a size is not correct and complete. If you make any changes to this, please keep in mind that it could affect database providers.

@ajcvickers ajcvickers self-assigned this Jun 12, 2020
@ajcvickers ajcvickers added this to the 5.0.0 milestone Jun 12, 2020
ajcvickers added a commit that referenced this issue Jul 28, 2020
@ghost ghost closed this as completed in #21829 Jul 28, 2020
ghost pushed a commit that referenced this issue Jul 28, 2020
…fied separately (#21829)

* Fully support base type mapping names with precision/scale/size specified separately

Fixes #21061
Fixes #21140
Fixes #15159

* Updates from review feedback
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-rc1 Aug 14, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants