2011-10-13 13 views
1

xxモデルのデータを初期化する必要がありますが、下のコードでわかるように、ProductImageのフィールドがあります。EF 4.1、画像が必要なときにデータを初期化する方法

イメージがローカルパス上にある場合は、とにかくデータベースにイメージをシードすることはありますか? 申込者クラスをご覧ください。

public class HRContext : DbContext 
    { 
     public DbSet<Position> Positions { get; set; } 
     public DbSet<Applicant> Applicants { get; set; } 
     public DbSet<ApplicantImage> ApplicantImages { get; set; } 
     public DbSet<ApplicantPosition> ApplicantsPositions { get; set; } 
     public DbSet<ApplicationPositionHistory> ApplicationsPositionHistory { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Position>().ToTable("Position"); 
      modelBuilder.Entity<Applicant>().ToTable("Applicant"); 
      modelBuilder.Entity<ApplicantPosition>().ToTable("ApplicantPosition"); 
      modelBuilder.Entity<ApplicationPositionHistory>().ToTable("ApplicationsPositionHistory"); 
      modelBuilder.Entity<Position>().Property(c => c.name).IsRequired(); 
      modelBuilder.Entity<Applicant>().Property(c => c.name).IsRequired(); 
      modelBuilder.Entity<ApplicantPosition>().Property(c => c.appliedDate).IsRequired(); 

      base.OnModelCreating(modelBuilder); 
     } 
    } 

    public class HRContextInitializer : DropCreateDatabaseAlways<HRContext> 
    { 
     protected override void Seed(HRContext context) 
     { 
      Position netdeveloper = new Position() { name = ".net developer", yearsExperienceRequired = 5 }; 
      Position javadeveloper = new Position() { name = "java developer", yearsExperienceRequired = 5 }; 

      Applicant luis = new Applicant() 
      { 
       name = "Luis", 
       skypeuser = "le.xx", 
       telephone = "345353453", 

      }; 

      Applicant john = new Applicant() 
      { 
       name = "John", 
       skypeuser = "jo.valencia", 
       telephone = "3435343543" 
      }; 


      ApplicantPosition appicantposition = new ApplicantPosition() 
      { 
       Applicant =luis, Position = netdeveloper, appliedDate=DateTime.Today,Status=Status.Applied 
      }; 

      context.Positions.Add(netdeveloper); 
      context.Positions.Add(javadeveloper); 
      context.Applicants.Add(luis); 
      context.Applicants.Add(john); 
      context.ApplicantsPositions.Add(appicantposition); 


     } 

    } 

    public class Position 
    { 
     public int PositionId { get; set; } 
     [StringLength(20, MinimumLength=3)] 
     public string name { get; set; } 
     public int yearsExperienceRequired { get; set; } 
     public virtual ICollection<ApplicantPosition> applicantPosition { get; set; } 
    } 

    public class Applicant 
    { 
     public int ApplicantId { get; set; } 
     [StringLength(20, MinimumLength = 3)] 
     public string name { get; set; } 
     public string telephone { get; set; } 
     public string skypeuser { get; set; } 
     public ApplicantImage photo { get; set; } 
     public virtual ICollection<ApplicantPosition> applicantPosition { get; set; } 

    } 

    public class ApplicantPosition 
    { 
     public int ApplicantId { get; set; } 
     public int PositionId { get; set; } 
     public virtual Position Position { get; set; } 
     public virtual Applicant Applicant { get; set; } 
     public DateTime appliedDate { get; set; } 
     public int StatusValue { get; set; } 

     public Status Status 
     { 
      get { return (Status)StatusValue; } 
      set { StatusValue = (int)value; } 
     } 

     //[NotMapped] 
     //public int numberOfApplicantsApplied 
     //{ 
     // get 
     // { 
     //  int query = 
     //    (from ap in Position 
     //    where ap.Status == (int)Status.Applied 
     //    select ap 
     //     ).Count(); 
     //  return query; 
     // } 
     //} 
    } 




    public class ApplicantImage 
    { 
     public int ApplicantId { get; private set; } 
     public byte[] Image { get; set; } 
    } 

    public class Address 
    { 
     [StringLength(20, MinimumLength = 3)] 
     public string Country { get; set; } 
     [StringLength(20, MinimumLength = 3)] 
     public string City { get; set; } 
     [StringLength(20, MinimumLength = 3)] 
     public string AddressLine1 { get; set; } 
     public string AddressLine2 { get; set; }  
    } 



    public class ApplicationPositionHistory 
    { 
     public ApplicantPosition applicantPosition { get; set; } 
     public Status oldStatus { get; set; } 
     public Status newStatus { get; set; } 
     [StringLength(500, MinimumLength = 10)] 
     public string comments { get; set; } 
     public DateTime dateModified { get; set; } 
    } 

    public enum Status 
    { 
     Applied, 
     AcceptedByHR, 
     AcceptedByTechnicalDepartment, 
     InterviewedByHR, 
     InterviewedByTechnicalDepartment, 
     InterviewedByGeneralManager, 
     AcceptedByGeneralManager, 
     NotAccepted 
    } 
+0

私の回答が役に立ったか、まだ動作していませんか? – nemesv

答えて

1

イメージを読んでバイト配列に入れましたか?

ApplicantImage jonhImage = new ApplicantImage() 
    { 
     Image = File.ReadAllBytes(filepath); 
    }; 

Applicant john = new Applicant() 
{ 
    name = "John", 
    skypeuser = "jo.valencia", 
    telephone = "3435343543" 
    photo = jonhImage 
}; 
... 
context.ApplicantImages.Add(jonhImage); 

例外はありますか?

関連する問題