简体   繁体   English

如何在Entity Framework 4.1的Code-First Fluent API中以编程方式定义关系

[英]How to define relationships programmatically in Entity Framework 4.1's Code-First Fluent API

I'm playing around with the new EF4.1 unicorn love. 我正在玩新的EF4.1独角兽之爱。

I'm trying to understand the different ways I can use code-first to programatically define my relationships between a few simple POCO's. 我试图理解不同的方法,我可以使用代码优先编程方式定义我的几个简单的POCO之间的关系。

How can I define the following => 如何定义以下=>

  1. 1 Team has 0-many User s. 1个Team有0个User (and a User is in 1 Team ) (并且User在一个Team
  2. 1 User has 0-or-1 Foo 's (but a Foo has no property going back to a User ) 1 User有0或1个Foo (但Foo没有属性返回给User
  3. 1 User has 1 UserStuff 1个User有1个UserStuff

Here you have examples you are looking for: 这里有你要找的例子:

public class User
{
    public int Id { get; set; }
    ...
    public Foo Foo { get; set; }
    public Team Team { get; set; }
    public UserStuff UserStuff { get; set; }
}

public class Team
{
    public int Id { get; set; }
    ...
    public ICollection<User> Users { get; set; }
}

public class Foo
{
    public int Id { get; set; }
    ...
}

public class UserStuff
{
    public int Id { get; set; }
    ...
}

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Team> Teams { get; set; }
    public DbSet<UserStuff> UserStuff { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .HasRequired(u => u.Team)
            .WithMany(t => t.Users);

        modelBuilder.Entity<User>()
            .HasOptional(u => u.Foo)
            .WithRequired();

        modelBuilder.Entity<User>()
            .HasRequired(u => u.UserStuff)
            .WithRequiredPrincipal();
    }
}

Lets introduce a few specific classes to illustrate the solutions: 让我们介绍一些特定的类来说明解决方案:

public class Account
{
    public long ID { get; set; }
    public virtual User User { get; set; }
}

public class User
{
    public long ID { get; set; }
    public virtual Account Account { get; set; }
    public virtual Team Team { get; set; }
}

public class Team
{
    public long ID { get; set; }
    public long CompanyID { get; set; }

    public virtual Company Company { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

public class Company
{
    public long ID { get; set; }
}

I'm using a helper class to make the mapping classes a bit less verbose: 我正在使用一个帮助器类来使映射类更简洁:

internal abstract class AbstractMappingProvider<T> : IMappingProvider where T : class
{
    public EntityTypeConfiguration<T> Map { get; private set; }

    public virtual void DefineModel( DbModelBuilder modelBuilder )
    {
        Map = modelBuilder.Entity<T>();

        Map.ToTable( typeof(T).Name );
    }
}

Now for the mappings. 现在为映射。 Lets do the "1:1" mapping first. 让我们先做“1:1”映射。 In my example user and account are 1:1 related and share the same primary key (only one of them will be an identity column, which in this case is the Account.ID). 在我的示例中,用户和帐户是1:1相关并共享相同的主键(其中只有一个是标识列,在本例中是Account.ID)。

internal class UserMapping : AbstractMappingProvider<User>
{
    public override void DefineModel( DbModelBuilder modelBuilder )
    {
        base.DefineModel( modelBuilder );
        Map.HasRequired( e => e.Account ).WithRequiredDependent( r => r.User ).WillCascadeOnDelete( true );
    }
}

internal class AccountMapping : AbstractMappingProvider<Account>
{
    public override void DefineModel( DbModelBuilder modelBuilder )
    {
        base.DefineModel( modelBuilder );
        Map.HasRequired( e => e.User ).WithRequiredPrincipal( r => r.Account ).WillCascadeOnDelete( true );
    }
}   

In the following mappings we specify that a team has (0..n) users, while a single user is in exactly one team (required). 在以下映射中,我们指定团队拥有(0..n)个用户,而单个用户只有一个团队(必需)。 We also specify that a team can have a company, but company does not expose a list of teams. 我们还指定团队可以拥有一家公司,但公司不公开团队列表。

internal class TeamMapping : AbstractMappingProvider<Team>
{
    public override void DefineModel( DbModelBuilder modelBuilder )
    {
        base.DefineModel( modelBuilder );
        Map.HasOptional( e => e.Company ).WithMany().HasForeignKey( e => e.CompanyID );
        Map.HasMany( e => e.Users ).WithRequired( r => r.Team );
    }
}   

internal class CompanyMapping : AbstractMappingProvider<Company>
{
    public override void DefineModel( DbModelBuilder modelBuilder )
    {
        base.DefineModel( modelBuilder );
    }
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM