外键 - 数据注释

时间:2015-04-07 11:37:00

标签: c# entity-framework

我一直在寻找解决方案近4个小时,但无法找到解决方案。

我有2个entites,我想使用dataannotations设置外键,麻烦(我认为)是基础实体。

public class BaseEntity
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public virtual int ID { get; set; }
        ...
}


public class Person : BaseEntity
{
    public string FirtName { get; set; }
    public string LastName { get; set; }

    public int? PersonAddressID { get; set; }
    public virtual PersonAddress PersonAddress { get; set; }

    public int? MainPersonID { get; set; }
    public virtual Person MainPerson { get; set; }

    public virtual HashSet<Projects.Project> ManagingProjects { get; set; }
    public virtual HashSet<Projects.Project> AdministratingProjects { get; set; }
    public virtual HashSet<Person> Dependants { get; set; }
    ...
}

public class PersonAddress : BaseEntity
{
    public int PersonID { get; set; }
    public virtual Person Person { get; set; }

    public string Street { get; set; }
    public string Town { get; set; }    
    ....
}

我尝试覆盖基本ID并添加了[Foreignkey(&#34; Person&#34;)],但仍然没有。我尝试了几种方法并遇到各种错误。

我也尝试了以下没有运气的事情

 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        [Column(Order = 1)]
        public override int ID { get; set; }

        [Key, Column(Order = 2), ForeignKey("Person")]
        public int PersonID { get; set; }

我试图弄清楚EF如何使用外键,但是我仍然坚持使用1到1或1到0关系在人和地址之间的第一个。

3 个答案:

答案 0 :(得分:0)

这是我定义类的方法:

public class BaseEntity
{ 
      //how about no key defined here  
      //[Key]
      //public virtual int ID { get; set; }

}


public class Person : BaseEntity
{
    public int PersonId { get; set; }

    public string FirtName { get; set; }
    public string LastName { get; set; }

    public virtual PersonAddress PersonAddress { get; set; }

    public int? MainPersonID { get; set; }
    public virtual Person MainPerson { get; set; }

    public virtual HashSet<Projects.Project> ManagingProjects { get; set; }
    public virtual HashSet<Projects.Project> AdministratingProjects { get; set; }
    public virtual HashSet<Person> Dependants { get; set; }
}

public class PersonAddress: BaseEntity
{
    [Key, ForeignKey("Person")]    
    public int PersonID { get; set; }
    public virtual Person Person { get; set; }

    public string Street { get; set; }
    public string Town { get; set; }    

}

答案 1 :(得分:0)

试试这个..

public class Person 
 {
   public int PersonID { get; set; }
   public string FirtName { get; set; }
   public string LastName { get; set; }

   public virtual PersonAddress PersonAddress { get; set; }
 }

 public class PersonAddress
 {
    [Key, ForeignKey("Person")]    
    public int PersonID { get; set; }   

    public string Street { get; set; }
    public string Town { get; set; }    

    public virtual Person Person { get; set; }
 }

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
    // Configure PersonId as PK for PersonAddress
    modelBuilder.Entity<PersonAddress>()
    .HasKey(e => e.PersonId);

   // Configure PersonId as FK for PersonAddress
     modelBuilder.Entity<Person>()
            .HasOptional(s => s.PersonAddress) // Mark PersonAddress is           optional for Person
            .WithRequired(ad => ad.Person); // Create inverse relationship

 }

答案 2 :(得分:0)

我不喜欢更改BaseEntity,因为我会丢失其他功能,例如断开连接的实体跟踪,以及丢失ID属性会限制泛型对我来说太多。但我知道必须离开并最终得到以下内容。

public class Person : BaseEntity
{
    public string FirtName { get; set; }
    public string LastName { get; set; }

    public virtual PersonAddress Address { get; set; }
    ...
}

public class PersonAddress : BaseEntity
{
    DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
    [Key, ForeignKey("Person")]
    public override int ID { get; set; }

    [ForeignKey("ID")]
    public virtual Person Person { get; set; }
    ...
}