How do you create an optional navigation property?

Viewed 75

I'm trying to create Code First DB in F# project and I just can't solve this problem. The idea is to create an optional relation and have a navigation property but if I have option property, EF won't create a migration:

The property 'Product.Model' is of type 'Model' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

If I ignore the property with Fluent API migration is created but it generates extra field and I get this warning:

The foreign key property 'Product.ModelId1' was created in shadow state because a conflicting property with the simple name 'ModelId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core.

Here is the examples of models:

type [<Table("products")>]
[<CLIMutable>]
Product = {
    [<Key>]
    [<Column("id")>]
    Id: Guid


    [<Column("name")>]
    [<Required>]
    Name: string

    //relations
    [<Column("model_id")>]
    ModelId: Guid option
    Model: Model option
}

and [<Table("models")>]
[<CLIMutable>]
Model = {
    [<Key>]
    [<Column("id")>]
    Id: Guid

    [<Column("name")>]
    [<Required>]
    Name: string

    //relations
    Products: Product seq
}

Using C#, adding virtual keyword would solve the issue, but is it possible with F#? Or should I use Nullable<> instead?

UPDATE

Ok, so the problem seems to be deeper as I can not see any way to create an optional relation in F# way at all! If I remove nav property, the EF keeps generating extra ModelId1 prop. At the moment, the only thing that seems to work for me is removing navigation property at all and using Nullable type for FK.

Is the only way to achieve my goal is to rewrite the whole DAL with C#?

0 Answers
Related