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

support for dotnet ef dbcontext scaffold #44

Open
bangonkali opened this issue Nov 13, 2024 · 2 comments
Open

support for dotnet ef dbcontext scaffold #44

bangonkali opened this issue Nov 13, 2024 · 2 comments

Comments

@bangonkali
Copy link

Hi! First of all, this is an awesome project. Thank you very much for making it available.

I would like to know if ef-core scaffolding is supported or planned to be supported. I'm trying to scaffold a table with a halfvec(3072) column and it's not working. I have the following table:

create table "memory-default"
(
    _pk       text                                          not null
        primary key,
    embedding halfvec(3072),
    labels    text[]                   default '{}'::text[] not null,
    chunk     text                     default ''::text     not null,
    extras    jsonb                    default '{}'::jsonb  not null,
    my_field1 text                     default ''::text,
    _update   timestamp with time zone default CURRENT_TIMESTAMP
);

alter table "memory-default"
    owner to postgres;

create index "memory-default_labels_idx"
    on "memory-default" using gin (labels);

create index "memory-default_embedding_idx"
    on "memory-default" using hnsw (embedding halfvec_cosine_ops);

I tried executing the following command:

dotnet ef dbcontext scaffold \
  --project src/MyProject.Core/MyProject.Core.csproj \
  --startup-project src/MyProject.Core/MyProject.Core.csproj \
  --data-annotations \
  --context MyProjectContext \
  --force \
  --output-dir Data/Entities \
  --namespace MyProject.Core.Entities \
  --context-namespace  MyProject.Core.Data \
  --context-dir Data \
  --no-onconfiguring \
  "Host=postgres;Port=5432;Username=postgres;Password=password;Database=myproject" \
  Npgsql.EntityFrameworkCore.PostgreSQL

And I got the following error:

Build started...
Build succeeded.
Could not load database collations.
Could not find type mapping for column 'public.memory-default.embedding' with data type 'halfvec(3072)'. Skipping column.
Unable to scaffold the index 'memory-default_embedding_idx'. The following columns could not be scaffolded: embedding.

The generated output only goes as far as the following in the DBContext:

public virtual DbSet<MemoryDefault> MemoryDefaults { get; set; }

And for the model iteself the output is:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using NodaTime;

namespace MyProject.Core.Entities;

[Table("memory-default")]
public partial class MemoryDefault
{
    [Key]
    [Column("_pk")]
    public string Pk { get; set; } = null!;

    [Column("labels")]
    public List<string> Labels { get; set; } = null!;

    [Column("chunk")]
    public string Chunk { get; set; } = null!;

    [Column("extras", TypeName = "jsonb")]
    public string Extras { get; set; } = null!;

    [Column("my_field1")]
    public string? MyField1 { get; set; }

    [Column("_update")]
    public Instant? Update { get; set; }
}

Btw, I'm using the NodaTime library for the Instant type. In the case of the Instant type NodaTime was able to replace the timestamp with time zone type from sql.

Maybe there is a way to implement proper mapping as well for scaffolding as NodaTime has?

@roji
Copy link
Contributor

roji commented Nov 13, 2024

Yeah, this shouldn't be too hard to do - I'll try to take care of this soon.

@bangonkali
Copy link
Author

Thanks @roji! 🙏 I have no background at the moment at this level of programming extensions/plugins with dotnet and efcore but I'm really interested myself to understand how this is going to be implemented. Looking forward to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants