EF 7 beta 6 : Entities with one to many relationships in EF 7

≯℡__Kan透↙ 提交于 2019-12-10 12:53:17

问题


I have a very simple model:

class Bag
{
    public int Id { get; set; }
    public ICollection<RandomThing> RandomThings { get; set; }
}

class RandomThing
{
    public int Id { get; set; }
    public String Description{ get; set; }
}

My context's OnModelCreating (in EF 6) is something like:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Bag>()
        .ToTable("Bag")
        .HasMany(x => x.RandomThings).WithRequired(x => x.Bag).WillCascadeOnDelete();

    modelBuilder.Entity<RandomThing>().ToTable("RandomThing");
}

As far as I can tell, there is no equivalent way to do this. I know that cascade deletes are not supported. However, I was wondering, what is the best way to model the one to many relationships as defined in my code example? It seems like all of that stuff has gone (or is yet to be implemented).

I have seen an example (here) of a DbContext exactly like how I want, but it's actually wrong and doesn't work. If I try to pull down that source code, set up my environment, and do a save on a blog entity with posts, those posts don't get saved as one might imagine.

Are there any workarounds for this?


回答1:


Update: This answer was written for EF7 beta7. APIs have since changed. See http://docs.efproject.net/en/latest/modeling/relationships.html for the latest on using EF Core.

Original Answer

In EF 7, you can configure one to many relationships in the OnModelCreating method. For this to work, add a property to RandomThing to represent the forign key, and also a CLR reference to the parent type.

class Bag
{
    public int Id { get; set; }
    public ICollection<RandomThing> RandomThings { get; set; }
}

class RandomThing
{
    public int Id { get; set; }
    public String Description{ get; set; }

    public Bag Bag { get; set; }
    public int BagId { get; set; }
}

In your context, configure the relationship with the following setup:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{  
    modelBuilder.Entity<RandomThing>()
        .Reference(e => e.Bag)
        .InverseCollection(p => p.RandomThings)
        .ForeignKey(e => e.BagId);
}

Side note: When EF 7 plans to add support for configuring this via attributes instead.




回答2:


although I haven't used EF7 at all, in EF6 I would make your models look like this:

class Bag
{
    public int Id { get; set; }
    public virtual ICollection<RandomThing> RandomThings { get; set; }
}

class RandomThing
{
    public int Id { get; set; }
    public String Description{ get; set; }

    public int BagId {get; set;}
    [ForeignKey("BagId")]
    Public Bag Bag {get; set;}
}

this way the relationship is taken care of with data annotations and you don't need the model builder for this specific task



来源:https://stackoverflow.com/questions/31943779/ef-7-beta-6-entities-with-one-to-many-relationships-in-ef-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!