-
Notifications
You must be signed in to change notification settings - Fork 28
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
Exception with optional navigation properties #29
Comments
cc @AndriySvyryd in case he has any pointers |
We would need to add explicit support for this in Core to get a nice experience. |
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? |
Seems like this could be solved by dotnet/efcore#22542 |
Thought I would post a small working sample since it took me a while to figure out how exactly
translates to code. [<AllowNullLiteral>]
type Comment() =
[<DefaultValue>] val mutable private _Id: int
// Backing field is a regular class which can be null
[<DefaultValue>] val mutable private _Parent: Comment
[<DefaultValue>] val mutable private _Text: string
member this.Id with get() = this._Id and set v = this._Id <- v
// Property is converted to and from Option
member this.Parent with get() = Option.ofObj this._Parent and set v = this._Parent <- Option.toObj v
member this.Text with get() = this._Text and set v = this._Text <- v
type CommentContext =
inherit DbContext
[<DefaultValue>] val mutable private _Comments: DbSet<Comment>
member this.Comments with get() = this._Comments and set v = this._Comments <- v
override this.OnModelCreating(modelBuilder: ModelBuilder) =
member this.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 |
Attempting to register optional properties automatically (#24) results in the following error in the Northwind database
Optional navigation properties need to be supported, mapping from
int option
toint
andTEntity option
toTEntity
will need to be addedThe text was updated successfully, but these errors were encountered: