Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Wednesday, December 20, 2017

Entity Framework Core 2 - not recognizing context for Core library

I'm used to having my EF entities in separate class library and having one solution just for this library and a unit-test project. It makes development much faster (quick compilation). After switching to EF Core (2), I ran into issue that migrations won't pick up DbContext and nothing would happen. So I tried running "Get-DbContext", which resulted in nothing - not even a message in the PMC console.
Interestingly enough, exactly the same code was working fine when the project type of the library was "Class Library (.NET Standard)" instead of "Class Library (.NET Core)".
After many experiments I figured out a way to make it work. It seems that it needs an "application" to be present in the solution for the migrations to work - could be web or console.
So, to fix this issue:

  1. Add "Console App (.NET Core)" to your solution
  2. Add EF Core nuget packages to the console app
  3. Reference your class library from the console app
  4. COMPILE!!
  5. Done - now you can generate migrations (don't forget to set your console app as a "StartupProject" either through parameter or in the PMC combo at the top)


Thursday, August 29, 2013

EF Code First AddOrUpdate puts null into referenced objects

I've ran into issue in my Seed() function, where I generate stub data.
This is my design:

    public class AircraftModel
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        public Guid ManufacturerId { get; set; }
        public virtual Manufacturer Manufacturer { get; set; }
    }

Now, I tried following code to generate data:

context.AircraftModels.AddOrUpdate(m => m.Name
  , new AircraftModel { 
        Name = "Global Express XRS", 
        Manufacturer = context.Manufacturers.Single(c => c.Name == "Bombardier Inc.") 
    }
);

It should work like charm I though, but it didn't. The problem was, that the Manufacturer was set to null after running AddOrUpdate - I guess because the ManufacturerId didn't match Manufacturer object.
When I changed to code to this, it worked fine:

context.AircraftModels.AddOrUpdate(m => m.Name
  , new AircraftModel { 
        Name = "Global Express XRS", 
        ManufacturerId = context.Manufacturers.Single(c => c.Name == "Bombardier Inc.").Id 
    }
);
Ok, so there's obvious problem with syncing the Manufacturer object and the ManufacturerId.
I've decided to keep them in sync using following modification:

    public class AircraftModel
    {
        private Manufacturer _manufacturer;

        public Guid Id { get; set; }

        public string Name { get; set; }

        public Guid ManufacturerId { get; set; }
        public virtual Manufacturer Manufacturer
        {
            get { return _manufacturer; }
            set
            {
                if (value != null && value.Id != ManufacturerId)
                    ManufacturerId = value.Id;
                _manufacturer = value;
            }
        }

    }

Now this code works as expected. Not sure if it's a bug or "feature"?...