Skip to content

Question: materializing one entity from multiple rows #3945

New issue

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

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

Already on GitHub? # to your account

Closed
natemcmaster opened this issue Dec 2, 2015 · 6 comments
Closed

Question: materializing one entity from multiple rows #3945

natemcmaster opened this issue Dec 2, 2015 · 6 comments

Comments

@natemcmaster
Copy link
Contributor

Q: Is an intermediate type the only way to materialize one entity from multiple rows?

Simplified case that represents the fundamental problem in materializing scaffolding's IndexModel and ForeignKeyModel with EF.

Table: Countries

Name Color
USA Red
USA White
USA Blue
public class Country
{
    [Key]
    public string Name { get; set; }

    public ICollection<string> Colors { get; set; }
}

Possible workaround:materialize Country by hand from intermediate type such as CountryRow.

@divega
Copy link
Contributor

divega commented Dec 2, 2015

@natemcmaster In your simplified example I see Colors is a collection of scalars which we don't support, but I am going to assume that this is just an artifact of the simplification.

Assuming this shape instead:

public class Country
{
    [Key]
    public string Name { get; set; }

    public ICollection<CountryColor> Colors { get; set; }
}


public class CountryColor 
{
    public string ColorName { get; set; }
    public string CountryName {get ; set; }
}
...
modelBuilder.Entity<CountryColor>().HasKey(cc = > new {cc.ColorName, cc.CountryName});

The queries would look something like this:

var Countries = context.Countries.FromSql("SELECT DISTINCT Name FROM Countries");
var CountryColors = context.CountryColors.FromSql("SELECT Name, Color FROM Countries");

@divega
Copy link
Contributor

divega commented Dec 2, 2015

Would that work?

@natemcmaster
Copy link
Contributor Author

@divega this solution is what I meant by "intermediate type". I was more asking if there were any plans to avoid this "middle man" type. If not, we can close and I can do the workaround approach.

collection of scalars which we don't support

I assume if we don't do a collection of scalars, a collection of related entity types is also out.

@divega
Copy link
Contributor

divega commented Dec 3, 2015

I assume if we don't do a collection of scalars, a collection of related entity types is also out

That is supported, but I am sure you know it 😄 so I must be missing what you mean.

Let's chat in person, otherwise I am afraid I may be misunderstanding what this issue is really about.

@natemcmaster
Copy link
Contributor Author

Discussed in person with @divega about different scenarios we support. We do not plan to support the scenario in the original question. Instead users should follow the suggestion outline above: see #3945 (comment)

@divega
Copy link
Contributor

divega commented Dec 7, 2015

Also, from talking to @natemcmaster I came to the conclusion that part of the problem was that our domain model for the database schema uses many-to-many relationships, which EF7 RTM won't support directly.

At least for the case of IndexModel and ColumnModel we know that the relationship we are trying to model actually contains payload (e.g. whether a column participates in the index as ASC or DESC, among other details) so the fix could be to include the "join" entity in our domain model.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants