MoonStorm / FastCrud

fast .NET ORM for strongly typed people
MIT License
506 stars 128 forks source link

ForeignKey on navigation property not work #182

Closed flier268 closed 1 year ago

flier268 commented 1 year ago
public class Parent
{
    [Key, Column(Order = 1)]
    public int Key1 { get; set; }

    [Key, Column(Order = 2)]
    public int Key2 { get; set; }

    public string Name { get; set; } = null!;

    [InverseProperty(nameof(Child.Parent))]
    public virtual IEnumerable<Child>? Children { get; set; }
}

public class Child
{
    [Key]
    public int Id { get; set; }

    public int ParentKey1 { get; set; }
    public int ParentKey2 { get; set; }

    [ForeignKey($"{nameof(ParentKey1)}, {nameof(ParentKey2)}")]
    public virtual Parent? Parent { get; set; }
}
MoonStorm commented 1 year ago

That is not a valid construct. You need to have a separate set of parent-child properties for each of foreign key relationships. Check the wiki for some examples.

flier268 commented 1 year ago

In EF Core, ForeignKey can be added to the child model or parent model, maybe we can support that. In some case, we will use Dapper and EF Core both with same model.