Open cspwizard opened 2 years ago
@cspwizard Are you saying that calling the migration "DateOnly" causes this behavior, as opposed to introducing a DateOnly
property in the EF model?
@ajcvickers exactly, migration called DateOnly cause this issue. ('DateOnly' migration generated without any issues, any next migration fails)
@roji This repros for me with the Npgsql provider, but only if there is both a DateOnly
property in the model, and the first migration is called DateOnly
.
public DateOnly DateOnly { get; set; }
With same model, this works:
PS C:\local\code\AllTogetherNow\SixOh> dotnet ef migrations add "One"
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
PS C:\local\code\AllTogetherNow\SixOh> dotnet ef migrations add "Two"
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
This does not:
PS C:\local\code\AllTogetherNow\SixOh> dotnet ef migrations add "DateOnly"
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
PS C:\local\code\AllTogetherNow\SixOh> dotnet ef migrations add "Two"
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Initialize(ColumnOperation columnOperation, IColumn column, RelationalTypeMapping typeMapping, Boolean isNullable, IEnumerable`1 migrationsAnnotations, Boolean inline)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IColumn source, IColumn target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(ITable source, ITable target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
PS C:\local\code\AllTogetherNow\SixOh>
Looks like it's because the migration references itself:
namespace Migrations
{
[DbContext(typeof(SomeDbContext))]
[Migration("20211210120821_DateOnly")]
partial class DateOnly
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Blog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Blogs");
});
modelBuilder.Entity("Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int?>("BlogId")
.HasColumnType("integer");
b.Property<string>("Content")
.HasColumnType("text");
b.Property<DateOnly>("DateOnly")
.HasColumnType("date");
b.Property<string>("Title")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("BlogId");
b.ToTable("Posts");
});
modelBuilder.Entity("PostTag", b =>
{
b.Property<int>("PostsId")
.HasColumnType("integer");
b.Property<int>("TagsId")
.HasColumnType("integer");
b.HasKey("PostsId", "TagsId");
b.HasIndex("TagsId");
b.ToTable("PostTag");
});
modelBuilder.Entity("Site", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int?>("BlogId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("BlogId")
.IsUnique();
b.ToTable("Site");
});
modelBuilder.Entity("Tag", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Content")
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Tag");
});
modelBuilder.Entity("Post", b =>
{
b.HasOne("Blog", "Blog")
.WithMany("Posts")
.HasForeignKey("BlogId");
b.Navigation("Blog");
});
modelBuilder.Entity("PostTag", b =>
{
b.HasOne("Post", null)
.WithMany()
.HasForeignKey("PostsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tag", null)
.WithMany()
.HasForeignKey("TagsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Site", b =>
{
b.HasOne("Blog", "Blog")
.WithOne("Site")
.HasForeignKey("Site", "BlogId");
b.Navigation("Blog");
});
modelBuilder.Entity("Blog", b =>
{
b.Navigation("Posts");
b.Navigation("Site");
});
#pragma warning restore 612, 618
}
}
}
Note from triage: consider throwing a better exception if a simple type name (e.g. "String", "Guid", "DateOnly") is used as the name of a migration.
I have also encountered this issue
Triage discussion - if we see other people hitting this, we may add something to the type name for the migration (e.g. the timestamp, like we already do in the file name).
Originally posted by @roji in https://github.com/dotnet/efcore/issues/25072#issuecomment-859754581
I had this problem with DateTimeOffset
too. Thanks for this.
Same with DateTime
Hit this issue this morning, thanks for creating a ticket so I had some idea where to start debugging from as the original System.NullReferenceException stacktrace wasn't helpful at all
File a bug
Remember:
Include your code
Any db context just add migration:
dotnet ef migrations add "DateOnly"
and try to add following one:
dotnet ef migrations add "DateOnly1"
Include stack traces
Include verbose output
Please include
--verbose
output when filing bugs about thedotnet ef
or Package Manager Console tools.Use triple-tick fences for tool output. For example:
Include provider and version information
EF Core version: 6.0.0 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL 6.0.1 Target framework: .NET 6.0 Operating system: Ubuntu 21.10 IDE: VS Code