0

So have an issue coming up with mapping where we have two classes

public class Customer
{
    public int Id;
    public CustomerAddress StatementAddress
    public bool AlwaysTrue => true;
    public string Code;
}

public class CustomerAddress
{
    public int Id;
    public bool ForStatement;
    public string _CustomerCode
}

We than have a mapping set up like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>()
        .HasOne(s => s.StatementAddress)
        .WithMany()
        .HasPrincipalKey(x=> new {x._CustomerCode, x.ForStatement})
        .HasForeignKey(s => new { s.Code, s.AlwaysTrue});
}

This is however causing an issue, as it seems to be on the database looking for a column of StatementAddress_CustomerCode and StatementAddress_ForStatement within the Customer table, which clearly are not things that exist - why isnt it looking for the Code table and simply passing through True for the AlwaysTrue?

I would have thought the way its defining keys would just Use the Code and Alwaystrue value, but seems to be looking for two columns made up of a combination of the Property name, appended with the property name from the other class =/

2 Answers 2

0

I would suggest two things here:

  1. You can either manually define column names like this:

    public class Customer
    {
        public int Id;
        public CustomerAddress StatementAddress;
        [Column("StatementAddress_ForStatement")]
        public bool AlwaysTrue => true;
        [Column("StatementAddress_CustomerCode")]
        public string Code;
    }
    
  2. You can specify the foreign key with the ForeignKeyAttribute like this:

    public class Customer
    {
        public int Id;
        public CustomerAddress StatementAddress;
        [ForeignKey("StatementAddress"), Column(Order = 1)]
        public bool AlwaysTrue => true;
        [ForeignKey("StatementAddress"), Column(Order = 0)]
        public string Code;
    }
    

    Keep in mind that for this option you have to set the column order attribute to match the one that the foreign key in the CustomerAddress table has.

Hope this helps!

3
  • Hey, marking the fields with a Column attribute wouldnt really help as it would just be specifying that those are the column names on the database - when they arent. marking individual attributes with foreign key seems a little odd to me? like according to the microsoft docs on EF core to set up a composite key you cant do it that way?
    – Gibbon
    Jul 23, 2018 at 14:35
  • If you are using code first migrations, with the column attribute those would become the column names. As far as the ForeignKey attribute goes, can you provide a link to the Microsoft docs part that says it cannot be used that way? Because as far as I can see, EF core supports the attribute. Jul 23, 2018 at 15:44
  • Hey, not using migrations at all - running in a Database first style as the database itself is actually 3rd party so non-editable or it would break other systems.
    – Gibbon
    Jul 23, 2018 at 16:01
0

So, to close this out -

This is something not currently possible in EF Core (2.1) and remains to be seen if it will be added in future versions - as present it only supports mapping through a single property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.