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

IsFixedLength() isn't propagated to foreign keys #18961

Closed
Neme12 opened this issue Nov 17, 2019 · 0 comments · Fixed by #19422
Closed

IsFixedLength() isn't propagated to foreign keys #18961

Neme12 opened this issue Nov 17, 2019 · 0 comments · Fixed by #19422
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@Neme12
Copy link

Neme12 commented Nov 17, 2019

A foreign key referencing a string column that has IsFixedLength set to true doesn't automatically receive IsFixedLength, resulting in a database error because the column types don't match.

Steps to reproduce

using Microsoft.EntityFrameworkCore;

namespace ConsoleApp3
{
    public sealed class Country
    {
        public string IsoCode { get; set; }
    }

    public sealed class Address
    {
        public int Id { get; set; }
        public string CountryCode { get; set; }
    }

    public sealed class ApplicationDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication-6E483A89-BEE6-4A76-96CC-CEB276E5E112;Trusted_Connection=True;MultipleActiveResultSets=true");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Country>().HasKey(c => c.IsoCode);

            modelBuilder.Entity<Country>().Property(c => c.IsoCode)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(2)
                .IsFixedLength(); // Removing this works.

            modelBuilder.Entity<Address>().Property(a => a.CountryCode)
                .IsRequired();

            modelBuilder.Entity<Address>().HasOne<Country>().WithMany().HasForeignKey(a => a.CountryCode).OnDelete(DeleteBehavior.Restrict);
        }
    }

    public static class Program
    {
        public static void Main()
        {
            using (var context = new ApplicationDbContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }
        }
    }
}

When running this, I get an exception from the database:

Microsoft.Data.SqlClient.SqlException: 'Column 'Country.IsoCode' is not the same data type as referencing column 'Address.CountryCode' in foreign key 'FK_Address_Country_CountryCode'.
Could not create constraint or index. See previous errors.'

If I remove IsFixedLength, everything works properly - both IsUnicode and HasMaxLength are set up on the foreign key to be the same as on the primary key - both columns end up being varchar(2). Since both of these are transferred automatically to the foreign key, I would expect that to be the case with IsFixedLength as well.

This isn't a critical issue of course because we can always specify IsFixedLength on the foreign key manually.

Further technical details

EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 Version 1903
IDE: Visual Studio 16.4.0 Preview 5.0

@ajcvickers ajcvickers added this to the 5.0.0 milestone Nov 18, 2019
@ajcvickers ajcvickers self-assigned this Nov 18, 2019
@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 Dec 29, 2019
ajcvickers added a commit that referenced this issue Dec 29, 2019
ajcvickers added a commit that referenced this issue Dec 30, 2019
ajcvickers added a commit that referenced this issue Dec 30, 2019
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview1 Mar 13, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview1, 5.0.0 Nov 7, 2020
@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. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants