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

DeleteBehavior is not updated to Cascade if requiredness is set on FK after the relationship is discovered #3460

Closed
divega opened this issue Oct 16, 2015 · 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

@divega
Copy link
Contributor

divega commented Oct 16, 2015

Deletes on required associations are supposed to cascade unless the user explicitly opts out. It seems that if the requiredness of the association is set after we first learn about it the delete behavior doesn't get updated.

Repro:

using System;
using System.Data.SqlClient;
using System.Linq;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Metadata;

namespace ConsoleApplication20
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (var context = new IdentityDbContext())
            {
                var fk =
                    context.Model.FindReferencingForeignKeys(context.Model.FindEntityType(typeof(Employee))).First();
                Console.WriteLine(
                    $"FK on Employee.Detail is required: {fk.IsRequired}");
                Console.WriteLine(
                    $"Delete behavior on Employee.Detail is {fk.DeleteBehavior}");  // Required
            }
        }
    }

    public class EmployeeDetail
    {
        public virtual string EmployeeId { get; set; }
        public virtual byte[] Photo { get; set; }
    }

    public class Employee
    {
        public virtual string Id { get; set; }
        public virtual EmployeeDetail Detail { get; set; }
    }

    public class IdentityDbContext : DbContext
    {
        public DbSet<Employee> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Employee>(b =>
            {
                b.HasOne(u => u.Detail)
                    .WithOne()
                    .HasForeignKey<EmployeeDetail>(pd => pd.EmployeeId);
            });

            // if key configuration of EmployeeDetail happens before the association configuration 
            // delete behavior will correctly be set to cascade 
            modelBuilder.Entity<EmployeeDetail>(b =>
            {
                b.HasKey(r => r.EmployeeId);
            });
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseLocalDB(GetType().Name);
        }
    }

    public static class DbContextOptionsExtensions
    {
        public static SqlServerDbContextOptionsBuilder UseLocalDB(this DbContextOptionsBuilder optionsBuilder,
            string databaseName = null, Action<SqlConnectionStringBuilder> action = null)
        {
            var connectionStringBuilder = new SqlConnectionStringBuilder
            {
                DataSource = "(localdb)\\mssqllocaldb",
                InitialCatalog = databaseName
            };
            action?.Invoke(connectionStringBuilder);
            return optionsBuilder.UseSqlServer(connectionStringBuilder.ConnectionString);
        }
    }
}
@rowanmiller rowanmiller added this to the 7.0.0 milestone Oct 20, 2015
@rowanmiller rowanmiller modified the milestones: 7.0.0-rc2, 7.0.0 Dec 11, 2015
AndriySvyryd added a commit that referenced this issue Dec 16, 2015
Make declaring a key on nullable properties to throw
Add conventions that run when a key is removed

Fixes #3460
AndriySvyryd added a commit that referenced this issue Dec 16, 2015
Make declaring a key on nullable properties to throw

Fixes #3460
AndriySvyryd added a commit that referenced this issue Dec 16, 2015
Make declaring a key on nullable properties to throw

Fixes #3460
@AndriySvyryd AndriySvyryd removed their assignment Apr 1, 2016
@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 Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
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

4 participants