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

User Defined Function in HasDefaultValueSql #24443

Closed
XiSigma opened this issue Mar 19, 2021 · 2 comments
Closed

User Defined Function in HasDefaultValueSql #24443

XiSigma opened this issue Mar 19, 2021 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@XiSigma
Copy link

XiSigma commented Mar 19, 2021

Consider the following where I create a sequence and set the default value of some property to an SQL User Defined Function call

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

      builder.HasSequence<long>("HumanIdSeq");

      builder.Entity<Order>()
             .Property(o => o.Reference)
             .ValueGeneratedOnAdd()
             .HasDefaultValueSql("dbo.GenerateHumanId(NEXT VALUE FOR HumanIdSeq)");
}

Given that I don't need to call my function from c# how can I create it as raw SQL using the ModelBuilder?

the only way I found is to modify the migration file and define my function as raw SQL there using the MigrationBuilder, is there a way to avoid this step and define it as raw SQL in the ModelBuilder?

@roji
Copy link
Member

roji commented Mar 19, 2021

Assuming you're using migrations, then yeah, the way to do this is to specify the raw SQL in your migration file (as in the docs). It currently isn't possible to define raw SQL to be run in model building without migrations - your only option is to include it in your migration file (or to run it outside of EF Core).

BTW I briefly mentioned specifying raw SQL without migrations back in the day in #6524 (comment), but I don't think we have a proper tracking issue). This wouldn't be trivial - it's easy enough to run the raw SQL on EnsureCreated, but if migrations are being used it's not clear what to do when the SQL changes...

@XiSigma XiSigma closed this as completed Mar 19, 2021
@roji roji added the closed-no-further-action The issue is closed and no further action is planned. label Mar 20, 2021
@roji
Copy link
Member

roji commented Mar 20, 2021

Opening to discuss in triage

@roji roji reopened this Mar 20, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants