Entity framework 6 code first - one way many to many via annotations

后端 未结 3 788
难免孤独
难免孤独 2021-01-02 00:35

Is it possible to create one-way many-to-many association in entity framework 6 with code first and annotations? Example:

class Currency
{
    public int id          


        
3条回答
  •  失恋的感觉
    2021-01-02 01:10

    You can do this in code first quite easily in EF 6.

        public class Country
    {
       public int ID {get;set;}
    
       public virtual ICollection Currencys {get;set;}//don't worry about the name,     pluralisation etc
    
    
    }
    
    public class Currency
    {
    
       public int ID {get;set;}
    
       public virtual ICollection Countrys {get;set;}//same as above - 
    
    }
    

    Compile it, run it and hey presto - magic join table in the background. Depends if the naming conventions bother you. I personally think if you are doing code first, you should do it all in the code. Some people prefer annotation, some prefer fluent API - use whichever you prefer.

提交回复
热议问题