实体框架多对多关系对象不可用

时间:2015-08-05 20:20:12

标签: c# asp.net-mvc entity-framework

添加迁移时出现此错误:

  

指定的架构无效。错误:关系   ' Answer_1_API.Infrastructure.ApplicationUser_Companies'没装   因为类型' Answer_1_API.Infrastructure.CompanyGroup'不是   可用。

我通过MSDN文章

将这个指南中的多对多关系松散地模仿了

ApplicationUser:

public class ApplicationUser : IdentityUser
    {
        [Required]
        [MaxLength(100)]
        public string FirstName { get; set; }

        [Required]
        [MaxLength(100)]
        public string LastName { get; set; }

        public virtual ICollection<StartelClientGroup> AssignedClients { get; set; }

        public virtual ICollection<CompanyGroup> Companies { get; set; }

        public ApplicationUser() 
        {
            AssignedClients = new HashSet<StartelClientGroup>();
            Companies = new HashSet<CompanyGroup>();
        }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
        {
            var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);

            return userIdentity;
        }
    }

小组课程:

public class Group
    {
        [Key]
        public Guid Id { get; set; }

        public virtual ICollection<ApplicationUser> Members { get; set; }

        [Required]
        [MaxLength(100)]
        public string Name { get; set; }

        public Group() 
        {
            Members = new HashSet<ApplicationUser>();
        }

    }

    public class CompanyGroup : Group
    {
        public CompanyGroup() : base() { }
    }

    public class StartelClientGroup : Group
    {
        public List<string> Values { get; set; }

        public Type ValueType { get; set; }

        public enum Type { ClientID, AccountNum, Affinity }

        public StartelClientGroup() : base() { }
    }

数据库上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) 
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }

        public static ApplicationDbContext Create() 
        {
            return new ApplicationDbContext();
        }

        public DbSet<StartelClientGroup> AssignedClients { get; set; }

        public DbSet<CompanyGroup> Companies { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
            modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
            base.OnModelCreating(modelBuilder);
        }
    }

关系配置:

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
    {
        public ApplicationUserConfiguration() 
        {
            this.HasMany(x => x.Companies)
                .WithMany(x => x.Members);

            this.HasMany(x => x.AssignedClients)
                .WithMany(x => x.Members);
        }
    }

谁能告诉我这里缺少什么?

0 个答案:

没有答案