EF 7测试版6:一对多的关系实体在EF 7 [英] EF 7 beta 6 : Entities with one to many relationships in EF 7

查看:220
本文介绍了EF 7测试版6:一对多的关系实体在EF 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的模型:

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; }
}



我的上下文的 OnModelCreating (在 EF 6 )是一样的东西:

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).

我见过一个例子(的这里)的的DbContext 酷似我多么希望,但它实际上是错误的,不工作。如果我试图拉下的源代码,建立了我的环境,也与职位博客实体保存,这些职位没有得到保存为人们想象的。

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?

推荐答案

更新:这个答案是为EF7写同β7。 API已经从此改变。请参见 http://docs.efproject.net/en/latest/modeling/relationships。 HTML 最新的关于使用EF核心。

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.

原来的答案

在EF 7,您可以配置一个在OnModelCreating方法很多关系。对于这项工作,将属性添加到R​​andomThing代表forign关键,也是一个CLR引用父类型。

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);
}

边注的:当EF 7的计划增加支持通过,而不是属性配置这一点。

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

这篇关于EF 7测试版6:一对多的关系实体在EF 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆