使用EF Code First进行M-M映射

时间:2014-10-07 15:21:46

标签: c# .net entity-framework ef-code-first

我有两个具有以下结构的课程

public class FeatureClaim : Entity<Guid>
    {

        public FeatureClaim()
        {
            ApplicationFeatures = new List<ApplicationFeature>();
        }

        public string ClaimType { get; set; }
        public string ClaimValue { get; set; }
        public ICollection<ApplicationFeature> ApplicationFeatures { get; protected set; }
    }





public class ApplicationFeature : Entity<Guid>
    {
        public ApplicationFeature()
        {
            FeatureClaims=new List<FeatureClaim>();
        }

        public string FeatureName { get; set; }
        public string FeatureCode { get; set; }
        public Guid ApplicationResourceId { get; set; }

        public ICollection<FeatureClaim> FeatureClaims { get; protected set; }
    }

这两个类有M-M关系,我通过流畅的api配置了这个关系。     配置看起来像。

public class ApplicationFeatureEntityConfiguration : EntityTypeConfiguration<ApplicationFeature>
    {

        public ApplicationFeatureEntityConfiguration()
        {
             HasKey(x => x.Id).Property(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.ApplicationResourceId).IsRequired();
            Property(x => x.FeatureCode).IsRequired().HasMaxLength(50);
            Property(x => x.FeatureName).IsRequired().HasMaxLength(150);

            HasMany(x => x.FeatureClaims).WithMany(z => z.ApplicationFeatures).Map(

              m =>
              {
                  m.MapLeftKey("ApplicationFeatureId");
                  m.MapRightKey("FeatureClaimId");
                  ToTable("ApplicationFeatureFeatureClaims");

              }
              );

            Property(x => x.RowVersion).IsRowVersion();
        }
    }
    public class FeatureEnityClaimConfiguration : EntityTypeConfiguration<FeatureClaim>
    {
        public FeatureEnityClaimConfiguration()
        {
             HasKey(x => x.Id).Property(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.RowVersion).IsRowVersion();

            Property(x => x.ClaimType).IsRequired().HasMaxLength(150);
            Property(x => x.ClaimValue).IsRequired().HasMaxLength(150);

            HasMany(x => x.ApplicationFeatures).WithMany(z => z.FeatureClaims).Map(

                m =>
                {
                    m.MapLeftKey("ApplicationFeatureId");
                    m.MapRightKey("FeatureClaimId");
                    ToTable("ApplicationFeatureFeatureClaims");

                }
                );
        }
    }

但是当我正在迁移时,我收到以下错误

实体类型'FeatureClaim'和'ApplicationFeature'不能共享表'ApplicationFeatureFeatureClaims',因为它们不在同一类型层次结构中,或者与匹配的主键没有有效的一对一外键关系他们之间。

这种配置有什么问题?如何通过更改我的配置/类定义来解决此问题。请注意,我不是在使用延迟加载。我应该实现这个流畅的api配置

0 个答案:

没有答案