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

后端 未结 2 1932
挽巷
挽巷 2021-02-01 23:43

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

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 23:57

    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 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 : IMappingProvider where T : class
    {
        public EntityTypeConfiguration Map { get; private set; }
    
        public virtual void DefineModel( DbModelBuilder modelBuilder )
        {
            Map = modelBuilder.Entity();
    
            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).

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

    internal class TeamMapping : AbstractMappingProvider
    {
        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
    {
        public override void DefineModel( DbModelBuilder modelBuilder )
        {
            base.DefineModel( modelBuilder );
        }
    }
    

    Hope this helps!

提交回复
热议问题