Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: HasNoKey and unit testing #18073

Closed
MCFHTAGENTS opened this issue Sep 26, 2019 · 6 comments
Closed

Question: HasNoKey and unit testing #18073

MCFHTAGENTS opened this issue Sep 26, 2019 · 6 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@MCFHTAGENTS
Copy link

With the move from DbQuery to DbSet and a call to HasNoKey for a view in MVC Core what is the expected pattern for seeding data for unit testing?

If I call the Add method on my DbSet<> object it fails Unable to track an instance of type xxx because it does not have a primary key. Only entity types with primary keys may be tracked.

Which makes sense. So is there a way of getting round this for the inmemory database?

@pranavkm pranavkm transferred this issue from dotnet/aspnetcore Sep 26, 2019
@smitpatel
Copy link
Contributor

In order to use keyless entity types in InMemoryDatabase you need to use defining queries to define how to get database for those query roots. Defining query is API which allows you to write linq query to generate database for given entityType based on other entityTypes in the model. It is SQL equivalent of view definition on the database.

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label Sep 27, 2019
@MCFHTAGENTS
Copy link
Author

Hi there - I can't see how this would work when by view is mapped in my ApplicationDbContext OnModelCreating or can I write code in there conditional on whether I am running the InMemoryDatabase or SQL server? The view is currently defined there as:

          builder.Entity<BookingView>().HasNoKey();
           builder.Entity<BookingView>().ToView("VW_BOOKINGS")
               .Property(v => v.BookingId).HasColumnName("BOOKING_ID");

My unit tests create an instance of the ApplicationDbContext and pass it to controllers etc. for testing.

@AndriySvyryd
Copy link
Member

@MCFHTAGENTS

if (Database.ProviderName == "Microsoft.EntityFrameworkCore.InMemory")
{
    ...
}
else
{
    ...
}

@MCFHTAGENTS
Copy link
Author

OK - i have the defining query working but, remembering the whole point of this is to seed data into my view for unit testing, have now run into the limitation that I can't use the Include construct for linked tables with defining queries... These are necessary for the SQL implementation of my code and so increasingly move my unit test away from testing the real code.

@AndriySvyryd
Copy link
Member

@MCFHTAGENTS That part would be covered by #17158

@TRegan3
Copy link

TRegan3 commented Feb 13, 2020

Could someone please post an example of what you mean by "defining query"?

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

5 participants