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

Generating Migrations loses using / namespace for custom types #5912

Closed
akuseru opened this issue Jun 30, 2016 · 10 comments
Closed

Generating Migrations loses using / namespace for custom types #5912

akuseru opened this issue Jun 30, 2016 · 10 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

@akuseru
Copy link

akuseru commented Jun 30, 2016

Steps to reproduce

when creating a model add a custom type. In my case i am using NpgsqlTypes.NpgsqlDateTime

using NpgsqlTypes;

// ---- OR ---

public NpgsqlTypes.NpgsqlDateTime Posted{get;set;}

(I have models with both methods atm for testing.)

The issue

I expect the command dotnet ef migrations add [migration name] to maintain the type definitions. either by adding a using NpgsqlTypes; or by keeping the type as NpgsqlTypes.NpgsqlDateTime

instead the following is generated:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using project.Data;

namespace project.Data.Migrations
{
    [DbContext(typeof(ApplicationDbContext))]
    [Migration("20160630040622_addNews")]
    partial class addNews
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
                 modelBuilder
                        .HasAnnotation("ProductVersion", "1.0.0-rtm-21431");
//------- snip ----
modelBuilder.Entity("project.Models.News", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd();

                    b.Property<string>("AuthorId");

                    b.Property<string>("Body")
                        .HasColumnType("text");

                    b.Property<int>("Comments");

                    b.Property<NpgsqlDateTime>("Posted");

                    b.Property<string>("SeoUrl");

                    b.Property<string>("Title");

                    b.Property<bool>("Visible");

                    b.HasKey("Id");

                    b.HasIndex("AuthorId");

                    b.HasIndex("SeoUrl");

                    b.ToTable("News");
                });
/// ---- snip ---
        }
    }
}

Exception message:
Stack trace:

/Users/adam/git/project/Data/Migrations/ApplicationDbContextModelSnapshot.cs(79,32): error CS0246: The type or namespace name 'NpgsqlDateTime' could not be found (are you missing a using directive or an assembly reference?)
/Users/adam/git/project/Data/Migrations/ApplicationDbContextModelSnapshot.cs(152,32): error CS0246: The type or namespace name 'NpgsqlDate' could not be found (are you missing a using directive or an assembly reference?)
/Users/adam/git/project/Data/Migrations/ApplicationDbContextModelSnapshot.cs(162,32): error CS0246: The type or namespace name 'NpgsqlDate' could not be found (are you missing a using directive or an assembly reference?)
/Users/adam/git/project/Data/Migrations/20160630040622_addNews.cs(20,43): error CS0246: The type or namespace name 'NpgsqlDateTime' could not be found (are you missing a using directive or an assembly reference?)
Compilation failed.
/Users/adam/git/project/Data/Migrations/20160630040622_addNews.Designer.cs(80,32): error CS0246: The type or namespace name 'NpgsqlDateTime' could not be found (are you missing a using directive or an assembly reference?)
    0 Warning(s)
    7 Error(s)
/Users/adam/git/project/Data/Migrations/20160630040622_addNews.Designer.cs(153,32): error CS0246: The type or namespace name 'NpgsqlDate' could not be found (are you missing a using directive or an assembly reference?)
Time elapsed 00:00:03.2841875
/Users/adam/git/project/Data/Migrations/20160630040622_addNews.Designer.cs(163,32): error CS0246: The type or namespace name 'NpgsqlDate' could not be found (are you missing a using directive or an assembly reference?)
Build failed on 'project'.

Further technical details

EF Core version: 1.0.0
Operating system:
Visual Studio version: VS code 1.2.1

Other details about my project setup:

"Npgsql": "3.1.5",
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
@natemcmaster
Copy link
Contributor

Can you use System.DateTime instead of NpgsqlDateTime? If I understand correctly, the Postgres provider will map this an appropriate storage type.

cc @roji @npgsql

@akuseru
Copy link
Author

akuseru commented Jun 30, 2016

I believe that I could, However there are still other types in the NpgsqlTypes that I would like to use, NpgsqlDate & NpgsqlPoint for instance. While I could simplify those down to more native types I already have existing data in those types that I would like to use.

Thanks!

@ajcvickers
Copy link
Contributor

Note for triage: We used to have code to do this for spatial types in the old stack. I think we should do the same thing here. We can't put model types in the snapshot because they will change as the model changes, but these types should be okay--we just need to handle them in the code that generates the using statements.

@bricelam
Copy link
Contributor

bricelam commented Jul 1, 2016

Interesting. I believe we have code to this this in the Migration class generator. We must have missed it in the snapshot.

@bricelam
Copy link
Contributor

bricelam commented Oct 6, 2016

We cover namespaces for annotation enum values, but it looks like we're missing namespaces for the following:

  • AddColumnOperation.ClrType
  • AlterColumnOperation.ClrType
  • AlterColumnOperation.OldColumn.ClrType
  • CreateTableOperation.AddColumnOperation.ClrType
  • IProperty.ClrType

@rowanmiller rowanmiller assigned ajcvickers and unassigned bricelam Oct 6, 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 11, 2016
@yukozh
Copy link
Contributor

yukozh commented Dec 27, 2016

@ajcvickers, @rowanmiller This issue still occurred in Pomelo.EntityFrameworkCore.MySql (1.1.0) When working with JsonObject

The generated script:

image

Model:

image

It seems that EF Core 1.1.0 cannot add the generic type's namespace yet.

@divega
Copy link
Contributor

divega commented Dec 27, 2016

@kagamine I am having a hard time trying to imagine how this class gets mapped, but if you can provide a repro that works (besides the namespaces not being generated in migrations) it would be great if you could create a new issue for it and we will investigate.

@ajcvickers
Copy link
Contributor

@divega It probably makes senses to look at the generic parameters regardless of whether the JsonObject stuff actually works right now. Re-opening to triage.

@ajcvickers
Copy link
Contributor

Decided to create a new issue (#7352) for this instead so we don't lose release information for where this one was fixed.

@divega
Copy link
Contributor

divega commented Jan 4, 2017

@ajcvickers makes sense. Thanks.

@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

7 participants