Adventures on the edge

Learning new development technologies
    "When living on the bleeding edge - you sometimes have to bleed" -BillKrat

No DbContext was found in assembly EF Core 2.0

Adventures on the edge EF Core 2.0 expects your “Application” to hold your data context (010-100 top left figure 2), but if you follow best practices and patterns your context will reside in the data layer (010-020-010 in green below figure 2) and you’ll receive this message when you try to “Add-Migration” via the Package Manager Console.

NoDbContext
Figure 1 Package Manager Console error message

I love Entity Framework Core 2.0 because our entities can remain POCO objects, effectively allowing us to decouple them from our layers (reference 010-030 ContactDonation.Model in middle-right of image below); later if I have to swap out the EF data access layer (010-020-010 in green below), for lets say an Azure data layer, my presentation and business logic layers won’t be affected.

It is quite problematic that EF Core 2.0 expects to see my data context in the main application (in my presentation layer).  Particularly since my data access layer for the ContactContext will be reused by other business logic layers, e.g., I might reuse it in a totally different application.  Note to mention that I may want to swap out my presentation layer (which has no business logic in it), e.g., for an ASP.NET application.  But I have a work-around….

UseCaseOverview

Figure 2 Separated presentation – MVPVM pattern (for decoupled frameworks)

By “linking” the ContactContext class from my data access layer project to my application (ContactDonationWpf), I can effectively fool Visual Studio into thinking the data context resides in the main application to complete processing.   This permits me to keep my data context neatly decoupled while allowing my POCO entities (below) in a separate ContactDonation.Model project that can be referenced by all layers.

namespace ContactDonation.Model
{
public class Contact
{
public string ContactId { get; set; } // PK
public string UserId { get; set; } // Users use
public string Email { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Middleinitial { get; set; }
public string NickName { get; set; }
public string Spouse { get; set; }
public string Realtives { get; set; }
public string Website { get; set; }

public ICollection<Address> Address { get; set; }
public ICollection<PhoneNumber> PhoneNumbers { get; set; }
public ICollection<Organization> Organizations { get; set; }
public ICollection<Tag> Tags { get; set; }
public string Notes { get; set; }

public Contact()
{
PhoneNumbers = new HashSet<PhoneNumber>();
Organizations = new HashSet<Organization>();
Tags = new HashSet<Tag>();
}
}
public class Address
{
public string AddressId { get; set; } // PK
public string Type { get; set; } // Billing, Residence
public string Street { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}

public class Organization
{
public string OrganizationId { get; set; } // PK
public ICollection<Address> Addresses { get; set; }
public ICollection<PhoneNumber> PhoneNumbers { get; set; }

public Organization()
{
Addresses= new List<Address>();
PhoneNumbers = new HashSet<PhoneNumber>();
}
}
public class PhoneNumber
{
public string PhoneNumberId { get; set; } // PK
public string Type { get; set; } // Cell, Work, Home
public string Number { get; set; }
}

public class Tag
{
public string TagId { get; set; } // PK
public string Name { get; set; }
public string Notes { get; set; }
}
}

Comments are closed