Influencing foreign key column naming in EF code first (CTP5)

后端 未结 1 866
囚心锁ツ
囚心锁ツ 2020-12-09 04:47

I have a POCO class that has two one-way unary relationships with another class, both classes share an ancestor. The names of the foreign keys in the generated schema do no

相关标签:
1条回答
  • 2020-12-09 05:07

    EF Code First uses, by default, convention over configuration. However, you can set explicit alternatives by overriding DbContent.OnModelCreating. Many examples here, courtesy of ScottGu.

    EDIT

    So in CTP5, MapSingleType went away as described here. The following works for simple string properties, but not for your Organisation to Person relationships. I'm curious and plan to keep looking at it, but in the meantime, maybe this will get your started or someone else can complete the answer.

    public class Person : Customer
    {
        [Column(Name="EmailAddress")]
        public string Email { get; set; }
    }
    

    EDIT 2

    Ok, this gets it. Found the answer here. Disclaimer: I've only verified that the database schema is created as expected. I have not tested that seeding data or further CRUD operations work as expected.

    public class Organisation : Customer
    {
        [Column(Name = "FinancialContact")]
        public int? FinancialContactId { get; set; }
        [ForeignKey("FinancialContactId")]
        public Person FinancialContact { get; set; }
        [Column(Name = "MainContact")]
        public int? MainContactId { get; set; }
        [ForeignKey("MainContactId")]
        public Person MainContact { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题