I\'m playing around with the new EF4.1 unicorn love.
I\'m trying to understand the different ways I can use code-first to programatically
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 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 Users { get; set; }
public DbSet Foos { get; set; }
public DbSet Teams { get; set; }
public DbSet UserStuff { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasRequired(u => u.Team)
.WithMany(t => t.Users);
modelBuilder.Entity()
.HasOptional(u => u.Foo)
.WithRequired();
modelBuilder.Entity()
.HasRequired(u => u.UserStuff)
.WithRequiredPrincipal();
}
}