Using: .NET 5 Preview 5.
My model has two int? (optional) values. When I try to seed without those values (optional) in an anonymous type, I get:
The number of values (9) doesn't match the number of columns (11) for the data insertion operation on 'customers'. Provide the same number of values and columns.
@mhosman can you please post a code sample?
Hey, @roji
This simple class:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int? DocumentTypeId { get; set; }
public bool Status { get; set; }
}
Try to seed data without specifying the DocumentTypeId (that is an optional value):
// Seed data
builder.HasData(
new {
TenantId = 1,
Id = 1,
Name = "Customer Name",
Status = true
}
);
I use an anonymous type because I have a multi-tenant app so I need to specif媒 the tenant id (that is not defined in the class, so is a shadow property).
In this case I'll get:
The number of values (3) doesn't match the number of columns (4) for the data insertion operation on 'customers'. Provide the same number of values and columns.
(Using provider 'Npgsql.EntityFrameworkCore.PostgreSQL')
UPDATE:
Also not working if I put the value as null:
// Seed data
builder.HasData(
new {
TenantId = 1,
Id = 1,
DocumentTypeId = (int?)null,
Name = "Customer Name",
Status = true
}
);
@mhosman I am not able to reproduce this--see my code below. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
```C#
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int? DocumentTypeId { get; set; }
public bool Status { get; set; }
}
public static class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
using (var context = new SomeDbContext())
{
var customer = context.Set<Customer>().First();
}
}
}
public class SomeDbContext : DbContext
{
private static readonly ILoggerFactory
Logger = LoggerFactory.Create(x => x.AddConsole()); //.SetMinimumLevel(LogLevel.Debug));
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Customer>()
.HasData(
new
{
TenantId = 1,
Id = 1,
Name = "Customer Name",
Status = true
}
);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(Logger)
.EnableSensitiveDataLogging()
.UseSqlServer(Your.SqlServerConnectionString);
}
```
Hey @ajcvickers thanks for your response!!
I'm using Npgsql.EntityFrameworkCore.PostgreSQL as provider, not SQL Server.
Maybe @roji is a bug in Npgsql.EntityFrameworkCore.PostgreSQL?
@mhosman This seems unlikely to be provider-specific. Before I run the code I posted above against PotsgreSQL, can I just check that you did attempt to do this already and it failed with the same exception you posted?
Hey @ajcvickers you're right! I tested both and I get the same error. This is only happening in new .NET 5. With .NET Core 3.1.5 it's working okay. I'll try to reproduce this issue in a smal project but I don't know exactly where the error is.
Good morning! I'm having the same issue in my project. I had to rollback to 3.1.5.
Hey @ajcvickers I had no time to make an example. I'll try to do that later. Anyway could you find any issue or clue?
@mhosman No, I don't have a clue.
Hey @ajcvickers, @roji I tested the same code you have and I get the same error:
WebApplication1.zip
Using Entity Framework Core .NET Command-line Tools
5.0.0-preview.5.20278.2
@roji @ajcvickers Tested with Preview 6 and the issue is still happening. Could you reproduce the issue with the file I left in the previous post? Thank you!
Note for triage: I am able to reproduce this. The generated migration contains:
C#
migrationBuilder.InsertData(
table: "Customer",
columns: new[] { "Id", "DocumentTypeId", "Name", "Status" },
values: new object[] { 1, "Customer Name", true });
Exactly @ajcvickers, migrations are being created in that way.
@AndriySvyryd Still repros on latest daily. Also, I can't see how this can be uncommon, so I'm putting it in 5.0 for now. (I may be missing something about why this doesn't happen more often.)
I have the same problem. Any news about it?
Still not working with preview 7. Any news? Thank you!!!!
Most helpful comment
Note for triage: I am able to reproduce this. The generated migration contains:
C# migrationBuilder.InsertData( table: "Customer", columns: new[] { "Id", "DocumentTypeId", "Name", "Status" }, values: new object[] { 1, "Customer Name", true });