我该如何使用EF code首先声明一个一对多的关系? [英] How can i use EF Code First to declare a one-to-many relationship?

查看:147
本文介绍了我该如何使用EF code首先声明一个一对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图定义一个简单的一一对多的关系的两个POCO的,使用实体框架流利的API。

I'm trying to define a simple one-to-many relationship between two poco's, using the Entity Framework fluent API.

~ Team ~
public int TeamId { get; set; }
public ICollection<User> TeamMembers { get; set; } // All the team players. Two way nav.
public Player CreatedBy { get; set; } // Which player created this team. One way navigation.
                                      // Optional. Not all players create a team.

~ Player ~
public int PlayerId { get; set; }
public Team Team { get; set; } // The other side of the TeamMembers navigation.

注:

  • 玩家不必在一队。 (未分配的/下降的球员)。
  • 在一个团队可能没有球员(他们都离开/退出)。

我想我得到的是这样的事情最近...

The closest I thought I got was something like this...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Team>()
        .HasOptional(x => x.TeamMembers)
        .WithMany()
        .WillCascadeOnDelete(false);
}

...不工作..和我不知道如何还定义了其他的资产净值。

... Which doesn't work .. and i'm not sure how to define the other navs also.

谁能帮助,请?

推荐答案

我觉得这个对象模型是你在找什么:

I think this object model is what you are looking for:

public class Team
{    
    public int TeamId { get; set; }
    public ICollection<Player> TeamMembers { get; set; } 
    public Player CreatedBy { get; set; } 
}

public class Player
{
    public int PlayerId { get; set; }
    public Team Team { get; set; } 
}       

public class Context : DbContext
{
    public DbSet<Player> Players { get; set; }
    public DbSet<Team> Teams { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Player>()
                    .HasOptional(p => p.Team)
                    .WithMany(t => t.TeamMembers)
                    .Map(c => c.MapKey("TeamId"));

        // Or alternatively you could start from the Team object:
        modelBuilder.Entity<Team>()
                    .HasMany(t => t.TeamMembers)
                    .WithOptional(p => p.Team)
                    .Map(c => c.MapKey("TeamId"));
    }
}

顺便说一句,您使用以下流利的API code是不正确的:

BTW, the following fluent API code that you are using is not correct:

...HasOptional(x => x.TeamMembers)

由于TeamMembers是一个集合,并且不能使用的 HasOptional 的方法,该方法总是具有与单个对象被调用。

Because TeamMembers is a collection and cannot be used by HasOptional method which always has to be invoked with a single object.

虽然他们都成立了一个协会,他们提供稍微不同的结果,并有不同的要求:

While they both set up an association, they deliver slightly different results and have different requirements:

  • 如果它是一个FK关联(FK的属性依赖对象暴露),那么它必须使用时要空类型的 HasOptional 的,当使用非可空类型的 HasRequired 的或code首先将抛出。

  • If it's a FK association (the FK property is exposed on the dependent object) then it must be a nullable type when using HasOptional and non-nullable type when using HasRequired or Code First will throw.

code首先会自动切换级联删除在使用上的 HasRequired 的方法。

Code First will automatically switch cascade deletes on when using HasRequired method.

另一个区别是EF运行时的行为,当谈到删除。考虑这样一个场景,我们要删除的主体对象(如团队),而它有依赖的对象(如播放 )和级联删除关闭。通过的 HasOptional 的EF运行时将依赖FK列默默地更新为空,同时与 HasRequired 的EF将会抛出,并要求您显式删除依赖对象,也涉及到另一主要目的(如果您想尝试这一点,你应该知道,在这两种情况下,本金和相关对象必须已经装载在上下文中,使EF都会有它们的跟踪)。

The other difference is the EF runtime behavior when it comes to deletion. Consider a scenario where we want to delete the principal object (e.g. Team) while it has a dependent object (e.g. Player) and cascade delete is switched off. With HasOptional EF runtime will silently update the dependent FK column to null while with HasRequired EF will throw and ask you to either explicitly remove the dependent object or relate it to another principal object (If you want to try this you should be aware that in both cases both principal and dependent objects must be already loaded in context so that EF will have a track of them).

这篇关于我该如何使用EF code首先声明一个一对多的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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