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

One-to-Many links or collections don’t work in beta7 #3104

Closed
poke opened this issue Sep 11, 2015 · 1 comment
Closed

One-to-Many links or collections don’t work in beta7 #3104

poke opened this issue Sep 11, 2015 · 1 comment

Comments

@poke
Copy link

poke commented Sep 11, 2015

I have a problem trying to set up a One-to-Many relationship with beta7. I have been trying different solutions now, e.g. as shown in #1368, or as shown in various tests like this one or the expected reverse engineered source.

So far, I couldn’t get the relationship to work completely. What works is that I can navigate from the dependent type up to the principal (in my example from Link up to Model). I can also insert dependent objects correctly when adding them to the principal’s collection (as shown in my example below).

But I just can’t get it to work that I get the collection filled with all dependent objects. So when I fetch a Model object from the database, the collection that contains the dependent Link objects is always empty. Although the database contains the elements just fine and although they were created using that exact path before.

So something is not right with the way the collection is set up. Is there a way in beta7 to make this work? Was this working before, or am I just doing something wrong here?


In order to fully reproduce this, these are my models and the respective db context in which I try to set up the relationship:

public class TestDbContext : DbContext
{
    public DbSet<Model> Models{ get; set; }
    public DbSet<Link> Links{ get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Model>().Collection(a => a.Links).InverseReference(l => l.Model).ForeignKey(l => l.ModelId);
        modelBuilder.Entity<Link>();
    }
}

public class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Link> Links { get; } = new List<Link>();
}

public class Link
{
    public int Id { get; set; }
    public int ModelId { get; set; }
    public Model Model { get; set; }
}

This is my Startup, in which I set up the db context, and have a very simple middleware that inserts some data initially and otherwise just displays the first model:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        string cs = "Server=(localdb)\\mssqllocaldb;Database=EfTest;Trusted_Connection=True";
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<TestDbContext>(options => options.UseSqlServer(cs));
    }

    public void Configure(IApplicationBuilder app, TestDbContext ctx)
    {
        app.Run(async (context) =>
        {
            Model m = ctx.Models.FirstOrDefault();
            if (m == null)
            {
                m = new Model() { Name = "foo" };
                ctx.Models.Add(m);
                m.Links.Add(new Link() { Model = m });
                m.Links.Add(new Link() { Model = m });
                await ctx.SaveChangesAsync();
            }

            string s = $"{m.Name} {string.Join(", ", m.Links.Select(x => x.Id))}";
            await context.Response.WriteAsync(s);
        });
    }
}

And for completeness, this is my project.json:

{
  "commands": {
    "ef": "EntityFramework.Commands"
  },
  "dependencies": {
    "EntityFramework.Commands": "7.0.0-beta7",
    "EntityFramework.SqlServer": "7.0.0-beta7",
    "EntityFramework.SqlServer.Design": "7.0.0-beta7",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta7"
  },
  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },
  "version": "1.0.0-*",
  "webroot": "wwwroot"
}

To reproduce this, the database needs to be created by running a first migration:

dnx ef migrations add Init
dnx ef database update
@poke
Copy link
Author

poke commented Sep 11, 2015

Oops, guess I became a victim of missing lazy loading which is not implemented as per #2980.

I just assumed that the code in this comment would be all that’s necessary to navigate.

When adding an Include(), everything worked just fine, so feel free to ignore me ;)

@poke poke closed this as completed Sep 11, 2015
@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants