You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
});
}
}
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 toModel
). 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 dependentLink
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:
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:And for completeness, this is my
project.json
:To reproduce this, the database needs to be created by running a first migration:
The text was updated successfully, but these errors were encountered: