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
Team
has 0-manyUser
s. (and aUser
is in 1Team
) - 1
User
has 0-or-1Foo
's (but aFoo
has no property going back to aUser
) - 1
User
has 1UserStuff
1个团队有0个用户。 (并且用户在1个团队中)
1用户有0或1个Foo(但Foo没有属性返回给用户)
1用户有1个UserStuff
2 个解决方案
#1
11
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();
}
}
#2
5
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. 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”映射。在我的示例中,用户和帐户是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). We also specify that a team can have a company, but company does not expose a list of teams.
在以下映射中,我们指定团队拥有(0..n)个用户,而单个用户只有一个团队(必需)。我们还指定团队可以拥有一家公司,但公司不公开团队列表。
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!
希望这可以帮助!
#1
11
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();
}
}
#2
5
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. 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”映射。在我的示例中,用户和帐户是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). We also specify that a team can have a company, but company does not expose a list of teams.
在以下映射中,我们指定团队拥有(0..n)个用户,而单个用户只有一个团队(必需)。我们还指定团队可以拥有一家公司,但公司不公开团队列表。
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!
希望这可以帮助!