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

EF Core Cosmos 7.0.13 => 7.0.14 Value cannot be null. (Parameter 'derivedType') #32363

Closed
PiotrKadziewicz opened this issue Nov 20, 2023 · 6 comments · Fixed by #32469
Closed
Labels
area-cosmos closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@PiotrKadziewicz
Copy link

PiotrKadziewicz commented Nov 20, 2023

Value cannot be null. (Parameter 'derivedType')

   at Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityType.IsAssignableFrom(IReadOnlyEntityType derivedType)
   at System.Linq.Enumerable.<SelectIterator>d__224`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosShapedQueryCompilingExpressionVisitor.CosmosProjectionBindingRemovingExpressionVisitorBase.PopulateCollection[TEntity,TCollection](IClrCollectionAccessor accessor, IEnumerable`1 entities)
   at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1.AsyncEnumerator.<MoveNextAsync>d__17.MoveNext()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__16`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__16`1.MoveNext()

Include your code

public class FilesType
    {
        public int Id { get; set; }
        public bool IsMain { get; set; }
        public string? Name { get; set; }
        public ICollection<Column>? Columns { get; set; }
        public ICollection<Column>? HeadersColumns { get; set; }

    }
    public class Column
    {
        public int Id { get; set; }
        public string? ColumnName { get; set; }
        public bool IsMain { get; set; }
        public int ColumnIndex { get; set; }
        public int CheckIndex { get; set; }
        public string? DateTimeFormat { get; set; }
        public string? Description { get; set; }
    }
public class ValidationEntityConfiguration : IEntityTypeConfiguration<ValidationSchema>
    {
        public void Configure(EntityTypeBuilder<ValidationSchema> builder)
        {
            builder.HasPartitionKey(f => f.Type);
            builder.HasKey(f => new { f.Id, f.Enviroment, f.Deleted });

            builder.OwnsMany(x => x.Files, files =>
            {
                files.OwnsMany(om => om.Columns);

                files.OwnsMany(om => om.HeadersColumns);
            });
        }
}

Error occurs on
public ICollection properties? Columns { get; set; }
public ICollection? HeadersColumns { get; set; }

If they are commented out or ef core is 7.013 or lower, the error does not occur

EF Core version: 7.0.14
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: .net 7
Operating system: Windows 11
IDE: (e.g. Visual Studio 2022 17.8)

@ajcvickers
Copy link
Member

@PiotrKadziewicz 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.

using (var context = new SomeDbContext())
{
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();

    context.Add(new ValidationSchema
    {
        Type = "X", Enviroment = "Y", Id = 1,
        Files = new List<FilesType>
            { new() { Columns = new List<Column>(), HeadersColumns = new List<Column>(), Id = 2 } }
    });

    await context.SaveChangesAsync();
}

using (var context = new SomeDbContext())
{
    var pool = context.Set<ValidationSchema>().ToList();
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseCosmos(
                    "https://localhost:8081",
                    "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                    "PointReads")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ValidationSchema>(builder =>
        {
            builder.HasPartitionKey(f => f.Type);
            builder.HasKey(f => new { f.Id, f.Enviroment, f.Deleted });

            builder.OwnsMany(x => x.Files, files =>
            {
                files.OwnsMany(om => om.Columns);

                files.OwnsMany(om => om.HeadersColumns);
            });
        });
    }
}

public class ValidationSchema
{
    public int Id { get; set; }
    public string Type { get; set; }
    public ICollection<FilesType>? Files { get; set; }
    public string Enviroment { get; set; }
    public bool Deleted { get; set; }
}

public class FilesType
{
    public int Id { get; set; }
    public bool IsMain { get; set; }
    public string? Name { get; set; }
    public ICollection<Column>? Columns { get; set; }
    public ICollection<Column>? HeadersColumns { get; set; }

}

public class Column
{
    public int Id { get; set; }
    public string? ColumnName { get; set; }
    public bool IsMain { get; set; }
    public int ColumnIndex { get; set; }
    public int CheckIndex { get; set; }
    public string? DateTimeFormat { get; set; }
    public string? Description { get; set; }
}

@PiotrKadziewicz
Copy link
Author

https://github.com/PiotrKadziewicz/testefcore This is the code pulled from the app. If you use EF Cosmos 8.0.0 for writing and reading it's not a problem. But when I write with 7.0.13 and read with 8.0.0, I get this error
image

@PiotrKadziewicz
Copy link
Author

PiotrKadziewicz commented Nov 21, 2023

Write difference between EF 7.0.13 and 8.0.0

[ { "Id": "e98b70a8-98f3-4002-84e4-f989c4e130d2", "Enviroment": "Y", "Deleted": false, "AgentId": null, "Discriminator": "ValidationSchema", "EditedBy": null, "ExampleFiles": null, "IsMain": false, "MainId": null, "MainSchemaName": null, "MainSchemaVersion": null, "Name": "EF 7.0.13", "ProcessId": null, "Type": "X", "Version": null, "id": "ValidationSchema|e98b70a8-98f3-4002-84e4-f989c4e130d2|Y|False", "Files": [ { "Description": null, "DisplayName": null, "DocTypeName": null, "FileName": null, "FlowDirectory": "In", "Headers": false, "IsMain": false, "Name": "Test", "Selected": false, "UniqueKey": null, "Columns": [ { "CanBeNegative": false, "CheckFieldIndex": null, "CheckIndex": 0, "ColumnIndex": 0, "ColumnName": "testColumn", "DataType": null, "DateTimeFormat": null, "DecimalPlace": null, "DefaultValue": null, "Description": null, "IsMain": false, "LinkedColumns": null, "MaxLength": 0, "MoreValuesTocheck": null, "PId": null, "PossibleValues": null, "RegexPattern": null, "Required": false, "RequiredEmpty": false, "TwoFieldsRequired": false, "ValueTocheck": null } ], "HeadersColumns": [ { "CanBeNegative": false, "CheckFieldIndex": null, "CheckIndex": 0, "ColumnIndex": 0, "ColumnName": "testColumn", "DataType": null, "DateTimeFormat": null, "DecimalPlace": null, "DefaultValue": null, "Description": null, "IsMain": false, "LinkedColumns": null, "MaxLength": 0, "MoreValuesTocheck": null, "PId": null, "PossibleValues": null, "RegexPattern": null, "Required": false, "RequiredEmpty": false, "TwoFieldsRequired": false, "ValueTocheck": null } ] } ], "_rid": "OXBRAN1sRy0IAAAAAAAAAA==", "_self": "dbs/OXBRAA==/colls/OXBRAN1sRy0=/docs/OXBRAN1sRy0IAAAAAAAAAA==/", "_etag": "\"d80d0180-0000-0c00-0000-655c62210000\"", "_attachments": "attachments/", "_ts": 1700553249 }, { "Id": "03f90c1a-b7ce-4840-a2f8-9b56706a3cee", "Enviroment": "Y", "Deleted": false, "AgentId": null, "Discriminator": "ValidationSchema", "EditedBy": null, "ExampleFiles": null, "IsMain": false, "MainId": null, "MainSchemaName": null, "MainSchemaVersion": null, "Name": "EF 8.0.0", "ProcessId": null, "Type": "X", "Version": null, "id": "ValidationSchema|03f90c1a-b7ce-4840-a2f8-9b56706a3cee|Y|False", "Files": [ { "Id": 1, "Description": null, "DisplayName": null, "DocTypeName": null, "FileName": null, "FlowDirectory": "In", "Headers": false, "IsMain": false, "Name": "Test", "Selected": false, "UniqueKey": null, "Columns": [ { "Id": 1, "CanBeNegative": false, "CheckFieldIndex": null, "CheckIndex": 0, "ColumnIndex": 0, "ColumnName": "testColumn", "DataType": null, "DateTimeFormat": null, "DecimalPlace": null, "DefaultValue": null, "Description": null, "IsMain": false, "LinkedColumns": null, "MaxLength": 0, "MoreValuesTocheck": null, "PId": null, "PossibleValues": null, "RegexPattern": null, "Required": false, "RequiredEmpty": false, "TwoFieldsRequired": false, "ValueTocheck": null } ], "HeadersColumns": [ { "Id": 1, "CanBeNegative": false, "CheckFieldIndex": null, "CheckIndex": 0, "ColumnIndex": 0, "ColumnName": "testColumn", "DataType": null, "DateTimeFormat": null, "DecimalPlace": null, "DefaultValue": null, "Description": null, "IsMain": false, "LinkedColumns": null, "MaxLength": 0, "MoreValuesTocheck": null, "PId": null, "PossibleValues": null, "RegexPattern": null, "Required": false, "RequiredEmpty": false, "TwoFieldsRequired": false, "ValueTocheck": null } ] } ], "_rid": "OXBRAN1sRy0JAAAAAAAAAA==", "_self": "dbs/OXBRAA==/colls/OXBRAN1sRy0=/docs/OXBRAN1sRy0JAAAAAAAAAA==/", "_etag": "\"d80da682-0000-0c00-0000-655c62530000\"", "_attachments": "attachments/", "_ts": 1700553299 } ]

image
image

@PiotrKadziewicz
Copy link
Author

removing "int Id" from models helped

@ajcvickers
Copy link
Member

Note for triage: this is an intentional change (#31664), but we should discuss how to deal with the breaking change where old documents will not load.

@PiotrKadziewicz For 7.0.14, you can set the following AppContext switch to the old behavior:

AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue31664", true);

This switch will not work on 8.0.

@PiotrKadziewicz
Copy link
Author

I removed the Id properties from the entity. From what I remember, it also had to be added because after an update ef core did not load them without ID. They are no longer needed. Message: no unique key. It's ok now, thanks

@ajcvickers ajcvickers added this to the 7.0.x milestone Nov 30, 2023
AndriySvyryd added a commit that referenced this issue Dec 1, 2023
@AndriySvyryd AndriySvyryd reopened this Dec 1, 2023
@AndriySvyryd AndriySvyryd modified the milestones: 7.0.x, 8.0.x Dec 1, 2023
AndriySvyryd added a commit that referenced this issue Dec 1, 2023
AndriySvyryd added a commit that referenced this issue Dec 1, 2023
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 2, 2023
@AndriySvyryd AndriySvyryd removed their assignment Dec 2, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.2 Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants