1

I need to create a database in asp.net core 2.2 .

I use this extension method for auto DBset in my project .

I use this code to add a Class to Dbset :

 public static void ApplyAllConfigrationEntities(this ModelBuilder builder, params Assembly[] assemblies)
    {
        MethodInfo applygenericMethod = typeof(ModelBuilder).GetMethods().First(x => x.Name == nameof(ModelBuilder));

        IEnumerable<Type> types = assemblies.SelectMany(x => x.GetExportedTypes())
            .Where(c => c.IsClass && !c.IsAbstract && c.IsPublic);

        foreach (Type type in types)
        {
            foreach (Type iface in type.GetInterfaces())
            {
                if (iface.IsConstructedGenericType && iface.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>))
                {
                    MethodInfo applyConcreteMethod = applygenericMethod.MakeGenericMethod(iface.GenericTypeArguments[0]);
                    applygenericMethod.Invoke(builder, new object[] { Activator.CreateInstance(type) });
                }
            }
        }
    }

    public static void RegisterAllEntites<BaseType>(this ModelBuilder builder, params Assembly[] assemblies)
    {
        IEnumerable<Type> types = assemblies.SelectMany(x => x.GetExportedTypes())
            .Where(x => x.IsClass && !x.IsAbstract && x.IsPublic && typeof(BaseType).IsAssignableFrom(x));

        foreach (Type item in types)
            builder.Entity(item);

    }

and this is the ApplicationDbContext:

      protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var entityAssembly = typeof(IEntity).Assembly;
        var ConfigorationAssembly = typeof(IType).Assembly;

        base.OnModelCreating(modelBuilder);
        modelBuilder.RegisterAllEntites<IEntity>(entityAssembly);
        modelBuilder.ApplyAllConfigrationEntities(ConfigorationAssembly);
    }

this is my classes :

 public class Category : BaseEntity
{
    public string Description { get; set; }
    [Column("ParentCategory")]
    public int? ParentCategoryId { get; set; }
    [ForeignKey("ParentId")]
    public Category ParentCategory { get; set; }
    public ICollection<Category> Categories { get; set; }
    public ICollection<News> News { get; set; }
}

 public class News : BaseEntity
{
    public string Title { get; set; }
    public int WriterId { get; set; }
    public DateTime CreateDate { get; set; }
    public string Description { get; set; }
    public int CategryId { get; set; }
    public string ImageName { get; set; }
    public User User { get; set; }
    public Category Category { get; set; }
}

 public class User : BaseEntity
{
    public User()
    {
        IsActive = false;
    }
    public string Name { get; set; }
    public string Family { get; set; }
    public string ImageName { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public string PasswordHashed { get; set; }
    public bool IsActive { get; set; }
    public GenderType Gender { get; set; }
    public ICollection<News> Posts { get; set; }
}

public enum GenderType
{
    [Display(Name = "مرد")]
    Male = 1,
    [Display(Name = "زن")]
    Female = 2
}

 public abstract class BaseEntity<TKey> : IEntity
{
    public TKey Id { get; set; }
}
public abstract class BaseEntity : BaseEntity<int>
{

}

and this is my Configuration in other assembly :

 public class CategoryConfiguration : IEntityTypeConfiguration<Category>, IType
{
    public void Configure(EntityTypeBuilder<Category> builder)
    {
        builder.HasOne(c => c.ParentCategory).WithMany(x => x.Categories).HasForeignKey(f => f.ParentCategoryId);
    }
}

    public class NewsConfiguration : IEntityTypeConfiguration<News>, IType
{
    public void Configure(EntityTypeBuilder<News> builder)
    {
        builder.HasOne(x => x.Category).WithMany(c => c.News).HasForeignKey(f => f.CategryId);
        builder.HasOne(x => x.User).WithMany(c => c.Posts).HasForeignKey(f => f.WriterId);
    }
}

but when i use the Add-Migration initial it show me this error :

Sequence contains no matching element

what's the problem ? how can I solve this problem ?

Falco Alexander
  • 3,092
  • 2
  • 20
  • 39
mr-dortaj
  • 782
  • 4
  • 11
  • 28
  • Your first line is looking for a method on the `ModelBuilder` class called `ModelBuilder` which is why you get that exception. – DavidG Mar 13 '19 at 12:12
  • `typeof(ModelBuilder).GetMethods().ToList()` will show you the actual methods you could call. – mjwills Mar 13 '19 at 12:14
  • 1
    Possible duplicate of [When to use .First and when to use .FirstOrDefault with LINQ?](https://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq) – mjwills Mar 13 '19 at 12:14
  • @mjwills Or you can just look at [the docs](https://learn.microsoft.com/en-us/ef/core/api/microsoft.entityframeworkcore.modelbuilder). – DavidG Mar 13 '19 at 12:15

0 Answers0