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

ChangeTracker shouldn't throw on conceptual nulls if one of the fk properties is nullable #6109

Closed
AndriySvyryd opened this issue Jul 19, 2016 · 0 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@AndriySvyryd
Copy link
Member

public class Product
{
    public int Id { get; set; }
    public int PartitionId { get; set; }

    public int? CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public int PartitionId { get; set; }

    public ICollection<Product> Products { get; set; }
}

public class StoreContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }

    public StoreContext(DbContextOptions options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .HasKey(p => new { p.Id, p.PartitionId });
        modelBuilder.Entity<Category>()
            .HasKey(c => new { c.Id, c.PartitionId });

        modelBuilder.Entity<Product>().HasOne(p => p.Category)
            .WithMany(c => c.Products)
            .HasForeignKey(p => new { p.CategoryId, p.PartitionId });
    }
}

context.Products.Add(new Product { Id = 1, PartitionId = 1, Category = new Category { Id = 2, PartitionId = 1 } });
context.SaveChanges();

context.Products.Include(p => p.Category).Single().Category = null;
context.SaveChanges();

Throws:
The association between entity types 'Product' and 'Category' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.

@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 20, 2016
ajcvickers added a commit that referenced this issue Jul 22, 2016
Issue #6109

Fix is to clear any conceptual null if any part of the composite key is nullable, and to instead make sure that that part is null.

Also added more coverage for update scenarios with composite keys and improved perf of GraphUpdatesTest.
ajcvickers added a commit that referenced this issue Jul 22, 2016
Issue #6109

Fix is to clear any conceptual null if any part of the composite key is nullable, and to instead make sure that that part is null.

Also added more coverage for update scenarios with composite keys and improved perf of GraphUpdatesTest.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 22, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants