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

EFCore 7 - SqlServer - DataAnnotation 'TypeName' and fluent API 'HasColumnType' is not being honored for aggregate type for which ToJson() is defined #29587

Closed
shvmgpt116 opened this issue Nov 16, 2022 · 1 comment

Comments

@shvmgpt116
Copy link

I am using a sample application where I have defined [Column(TypeName = "VARCHAR(500)")] data annotation for entity property 'Contact'.

public class Author
        {
            public int Id { get; set; }
            [Column(TypeName = "VARCHAR(20)")]
            public string Name { get; set; }

            [Column(TypeName = "VARCHAR(500)")]
            public ContactDetails Contact { get; set; }
        }

EnsureCreated() generates following create table script where column Name honors data annotation but column Contact does not.

CREATE TABLE [Authors] (
          [Id] int NOT NULL IDENTITY,
          [Name] VARCHAR(20) NOT NULL,
          [Contact] nvarchar(max) NOT NULL,
          CONSTRAINT [PK_Authors] PRIMARY KEY ([Id])
      );

Using fluent API

modelBuilder.Entity<Author>().Property(p => p.Contact).HasColumnType("VARCHAR(500)");

instead of data annotation is giving following error -
Contact' cannot be used as a property on entity type 'Author' because it is configured as a navigation.

Here is my OnModelCreating() method-

protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Author>().OwnsOne(
                    author => author.Contact, ownedNavigationBuilder =>
                    {
                        ownedNavigationBuilder.ToJson();
                        ownedNavigationBuilder.OwnsOne(contactDetails => contactDetails.Address);
                    });
                modelBuilder.Entity<Author>().Property(p => p.Contact).HasColumnType("VARCHAR(500)");
            }

Please could someone clarify if default data types can be changed for JSON/Aggregate types or not?
If yes, what am I missing in my sample application.
If no, are there any future plans on supporting this?

Sample application

using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace test
{
    class Program
    {
        public class ContactDetails
        {
            public Address Address { get; set; } = null!;
            public string? Phone { get; set; }
        }

        public class Address
        {
            public Address(string street, string city, string postcode, string country)
            {
                Street = street;
                City = city;
                Postcode = postcode;
                Country = country;
            }

            public string Street { get; set; }
            public string City { get; set; }
            public string Postcode { get; set; }
            public string Country { get; set; }
        }

        public class Author
        {
            public int Id { get; set; }
            [Column(TypeName = "VARCHAR(20)")]
            public string Name { get; set; }

            [Column(TypeName = "VARCHAR(500)")]
            public ContactDetails Contact { get; set; }
        }

        public class AuthorContext : DbContext
        {
            public DbSet<Author> Authors { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(@"<connection_string>").LogTo(Console.WriteLine);
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Author>().OwnsOne(
                    author => author.Contact, ownedNavigationBuilder =>
                    {
                        ownedNavigationBuilder.ToJson();
                        ownedNavigationBuilder.OwnsOne(contactDetails => contactDetails.Address);
                    });
                //modelBuilder.Entity<Author>().Property(p => p.Contact).HasColumnType("VARCHAR(500)");
            }

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

Provider and version information

EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.2.6

@maumar
Copy link
Contributor

maumar commented Nov 16, 2022

dupe of #28452

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants