locked
Error while Applying EF Code First Migration in .NET Core -- RRS feed

  • Question

  • User-215451226 posted

    Hi.

    This issue could be related to .NET Core or could also be related with EF. Or both. So I am posting it here. Basically I am trying to create my application database through code-first migration. My model classes structure is as follows --

    BaseModel --

    public class BaseModel
        {
            [Key]
            public long Id { get; set; }
        }

    Breed --

    public class Breed:BaseModel
        {
            public string Name { get; set; }
            public Animal Animal { get; set; }
        }

    Animal --

    public class Animal:BaseModel
        {
            public string Name { get; set; }
        }

    Pet --

    public class Pet:BaseModel
        {
            public string Name { get; set; }
            public DateTime? Birthday { get; set; }
            public Breed Breed { get; set; }
            public string ImageUrl { get; set; }
        }
    
        public enum Size {
            Unspecified = 0,
            Small = 1,
            Medium = 2,
            Large = 3
        }

    PetBreed --

    public class PetBreed:BaseModel {
           
            [ForeignKey("PetId")]
            public Pet Pet { get; set; }
            public long PetId { get; set; }
    
    
            [ForeignKey("BreedId")]
            public Breed Breed { get; set; }
            public long BreedId { get; set; }
        }

    In my Startup.cs I have set up my db and EF related matters as --

    private static readonly string PET_DB_CONNECTION_STRING = @"Server=.\SQLEXPRESS;Database=petstoredb;trusted_connection=true;Persist Security Info=True;Integrated Security=true;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
    
    
        public void ConfigureServices(IServiceCollection services) {
              services.AddMvc();
            services.AddDbContext<PetStoreContext>(options => options.UseSqlServer(PET_DB_CONNECTION_STRING));
            services.AddScoped<PetService>();
            services.AddScoped<BreedService>();
            services.AddScoped<AnimalService>();
        }

    My dbcontext file --

    public class PetStoreContext : DbContext
        {
            public PetStoreContext(DbContextOptions options) : base(options) { }
    
            public DbSet<Pet> Pets { get; set; }
            public DbSet<Breed> Breeds { get; set; }
            public DbSet<Animal> Animals { get; set; }
    
    
    
        }

    Now, as I go ahead and try to apply migration with add-migration IniCreate

    it gives me error:

    : Exception calling "AddFromFile" with "1" argument(s): "'basePath' cannot be an empty string ("") or start with the null character.
    Parameter name: basePath"
    At line:1 char:1



    Why is it happening so? I have tried a few alternatives during applying migration like
    add-migration -Name "InitialCreate"
    add-migration "InitialCreate" --verbose

    No luck. Also tried tweaking around with the connection string. Although I am pretty sure, there's not much wrong with it. It has to be the way I am structuring my model entities. That's my gut feeling, however weird it may feel.
    Can you pl tell me, what is it? How to rectify or edit the code so the migration actually works? Any changes in base of the constructor of dbcontext needed? Please help,
    Thanks,

    Belated Happy New Year to you All,

    Thursday, January 3, 2019 11:17 AM

Answers

  • User-215451226 posted

    Hi @mgebhard
    thanks so much or replying,

    Yes, in hindsight I have in fact messed up the inheritance structure for the model entities. No need for such over-engineering and that too faulty. I cleaned it up. rewrite it using simple approach how we usually normally write them.

    BUT.
    HERE.
    THIS SPECIFIC PROBLEM.

    Just got solved as I upgraded my .Net Core App Version and EF Core Versions to 2.0. That's It!!
    Previously they were 1.1. That immediately ran the migrations as you expect them to.

    Thanks for forwarding this link specially -- https://docs.microsoft.com/en-us/ef/core/modeling/inheritance
    It's very helpful for me. I am going to read it well,

    Thanks,
    :  )

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 4, 2019 7:09 AM

All replies