You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Attempting to register optional properties automatically (#24) results in the following error in the Northwind database
System.InvalidOperationException:
The relationship from 'Products.Category' to 'Categories.Products' with foreign key properties {'CategoryId' : FSharpOption<short>} cannot target the primary key {'CategoryId' : short} because it is not compatible.
Configure a principal key or a set of compatible foreign key properties for this relationship.
Optional navigation properties need to be supported, mapping from int option to int and TEntity option to TEntity will need to be added
We would need to add explicit support for this in Core to get a nice experience.
The best workaround I can think of is to use a Nullable private field that is wrapped by the optional property. Then configure the relationship to use the field and ignore the property.
Nullable private fields wouldn't be able to work on an F# record types, similar to C# records, private fields do not exist in them
Given that C# records are now a thing, and reference types in C# can now be annotated in not nullable, is there any support for optional types similar to what is planned for record types in dotnet/efcore#11457?
Thought I would post a small working sample since it took me a while to figure out how exactly
The best workaround I can think of is to use a Nullable private field that is wrapped by the optional property. Then configure the relationship to use the field and ignore the property.
translates to code.
[<AllowNullLiteral>]typeComment()=[<DefaultValue>]val mutableprivate_Id:int// Backing field is a regular class which can be null[<DefaultValue>]val mutableprivate_Parent:Comment[<DefaultValue>]val mutableprivate_Text:stringmemberthis.Idwith get()= this._Id andset v = this._Id <- v
// Property is converted to and from Optionmemberthis.Parentwith get()= Option.ofObj this._Parent andset v = this._Parent <- Option.toObj v
memberthis.Textwith get()= this._Text andset v = this._Text <- v
typeCommentContext=inherit DbContext
[<DefaultValue>]val mutableprivate_Comments:DbSet<Comment>memberthis.Commentswith get()= this._Comments andset v = this._Comments <- v
overridethis.OnModelCreating(modelBuilder:ModelBuilder)=memberthis.Configure(builder)=// Tell EF Core to ignore the property and use the field directly for the relationship
builder.Ignore("Parent")|> ignore
builder.HasOne("_Parent")
.WithMany()// Set the key you want to appear in the database
.HasForeignKey("ParentId")|> ignore
Activity
bricelam commentedon May 2, 2019
cc @AndriySvyryd in case he has any pointers
AndriySvyryd commentedon May 2, 2019
We would need to add explicit support for this in Core to get a nice experience.
The best workaround I can think of is to use a
Nullableprivate field that is wrapped by the optional property. Then configure the relationship to use the field and ignore the property.simon-reynolds commentedon Mar 18, 2021
Nullableprivate fields wouldn't be able to work on an F# record types, similar to C# records, private fields do not exist in themGiven that C# records are now a thing, and reference types in C# can now be annotated in not nullable, is there any support for optional types similar to what is planned for record types in dotnet/efcore#11457?
simon-reynolds commentedon Apr 4, 2021
Seems like this could be solved by dotnet/efcore#22542
LiteracyFanatic commentedon Jul 9, 2021
Thought I would post a small working sample since it took me a while to figure out how exactly
translates to code.