实体框架从一个到多个的单向关联

时间:2013-11-06 11:03:40

标签: c# entity-framework associations one-to-many

我有一个帐户类,用于表明谁对某个实体负责。将有许多实体将使用它,所以我不想污染我的帐户类与所有这些集合

public class Account
{
   public Guid Id{get; set;}
   public Guid Name{get; set;}
   ...
   public class EntityConfiguration : EntityConfigurationBase<Account>
   {   
       public EntityConfiguration()
       {
           // I do not want these!
           HasMany(a => a.As)   
              .WithOptional(x => x.Account)
              .HasForeignKey(x =>x.AccountKey);
            }
        }
    }
}


public class A 
{
   public Guid Id {get; set;}
   public Account Account{get; set;}
   // FK-Nav property
   public Guid AccountKey{get;set;}
   public class EntityConfiguration : EntityConfigurationBase<A>
   {   
       public EntityConfiguration()
       {
           // what should go here to specify the association to Account?
           ????
        }
    }
}

public class B 
{
   public Guid Id {get; set;}
   public Account Account{get; set;}
   // FK-Nav property
   public Guid AccountKey{get;set;}
      public class EntityConfiguration : EntityConfigurationBase<B>
   {   
       public EntityConfiguration()
       {
           // what should go here to specify the association to Account?
           ????
        }
    }

}

etc.

1 个答案:

答案 0 :(得分:0)

public class A 
{
   public Guid Id {get; set;}

   public Account Account{get; set;}
   // FK-Nav property
   public Guid AccountKey{get;set;}
   public class EntityConfiguration : EntityConfigurationBase<A>
   {   
       public EntityConfiguration()
       {

           HasOptional(x => x.Account).HasMany().HasForeignKey(x=>x.AccountKey);
        }
    }
}