EF中关系映射问题

一对一,和一对多的简单问题就部说了,直接来多对多这样的问题吧。

首现关系映射为这样的:

    /// <summary>
    /// 对应数据库中dbo.Address表
    /// </summary>
    [DataContract]
    [Table("Address", Schema = "dbo")]
    public class AddressInfo
    {
        public AddressInfo()
        {
            Province = new HashSet<ApplyAddress>();
            City = new HashSet<ApplyAddress>();
        }
        [DataMember]
        [Key]
        public int AddressId { get; set; }
            #region Relations
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ApplyAddress> Province { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ApplyAddress> City { get; set; }
        #endregion
    }

  

    /// <summary>
    /// 对应数据库中Apply.Address表
    /// </summary>
    [Table("Address", Schema = "Apply")]
    public class ApplyAddress : IEntity
    {
        [Key, Column(Order = 0)]
        [ForeignKey("Apply")]
        public int ApplyId { get; set; }
        [Key, Column(Order = 1)]
        [ForeignKey("Province")]
        public int ProvinceId { get; set; }
        [Key, Column(Order = 2)]
        [ForeignKey("City")]    
        public int CityId { get; set; }
        #region Relations
        public virtual ApplyInfo Apply { get; set; }
        public virtual AddressInfo Province { get; set; }
        public virtual AddressInfo City { get; set; }
        #endregion

    }

  

     /// <summary>
    /// 对应数据库中 dbo.UserApply表
    /// </summary> 
   [Table("UserApply", Schema = "dbo")]
    public class ApplyInfo : IEntity
    {
        public ApplyInfo()
        {
            Addresses = new HashSet<ApplyAddress>();
        }
        [Key]
        public int ApplyId { get; set; }

        public System.Guid UserId { get; set; }
       #region Relations
        [NotMapped]
        public virtual JobPositionCategory Category { get; set; }
        [NotMapped]
        public virtual ICollection<ApplyAddress> Addresses { get; set; }
        #endregion
    }

  这打致就是这三个表的结构的定义,dbo.address 的主键AddressID 和dbo.UserApply 的主键ApplyId 分别构成了Apply.Address 的三个主键(AddressId对应键ProvinceID和Cityid,Applyid对应键applyInfo),说明一下:我的AddressId 在数据库中表示的是省份和市都在一张表中表示,用的一个ParentId字段进行表示的,所以我这里的一个字段对应两个主键。

这个写逻辑上没有问题,可是Ef并不能帮我们识别,我们需要重写继承自DbCoontext的类中进行方法的重写,我们自己进行数据的定义

       protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ApplyAddress>().HasKey(t => new { t.ApplyId, t.CityId, t.ProvinceId });
            modelBuilder.Entity<ApplyAddress>()
                .HasOne(address => address.Apply)
                .WithMany(a => a.Addresses)
                .HasForeignKey(a => a.ApplyId);
            modelBuilder.Entity<ApplyAddress>()
                .HasOne(address => address.Province)
                .WithMany(a => a.Province)
                .HasForeignKey(a => a.ProvinceId);
            modelBuilder.Entity<ApplyAddress>()
                .HasOne(address => address.City)
                .WithMany(a => a.City)
                .HasForeignKey(a => a.CityId);
            base.OnModelCreating(modelBuilder);
        }

  首先我们表明,我们数据库中这个三个字段都是主键,接着我们定义映射关系。

posted @ 2017-07-02 10:10  Bluto  阅读(616)  评论(0编辑  收藏  举报