title: An exception occurred while initializing the database with data
body: <p>I am preparing a rest API with ASP.net</p>
  
  <p>I started initializing the database tables with data, but I get this error:</p>
  
  <p><code>An exception occurred while initializing the database</code>.</p>
  
  <p>The problem is that the data is in tables, only one table "Event", in which I get this problem, this is my code to initialize the database:</p>
  
  <pre><code> var FeedTypes = new List&lt;FeedType&gt;  
   {  
     new FeedType{Libel="FeedType1"},  
   };  
  
   FeedTypes.ForEach(s =&gt;  
   {  
     s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;  
     context.FeedTypes.Add(s);  
     context.SaveChanges();  
  
   });  
  
   var feedtypeee = context.FeedTypes.Where(s =&gt; s.Libel == "FeedType1").First().Id;  
   var Feeds = new List&lt;Feed&gt;  
   {  
      new Feed{Libel="Libel1", Content="contentnotif1" , CreatedDate=Convert.ToDateTime("2013-04-24 17:47:03"), UserSMAId=ccv, FeedTypeId=feedtypeee , ImageId=context.Images.Where(i=&gt;i.Alt=="Image 1").FirstOrDefault().Id},  
   };  
  
   Feeds.ForEach(s =&gt;  
   {  
     s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;  
     context.Feeds.Add(s);  
     context.SaveChanges();  
   });  
  
   var FeedComments = new List&lt;FeedComment&gt;  
   {  
      new FeedComment{Content="Content1", CreatedDate=Convert.ToDateTime("2013-04-24 17:47:03"), FeedId=context.Feeds.Where(s=&gt;s.Libel=="Libel1").FirstOrDefault().Id, UserSMAId=ccv},       
   };  
  
   FeedComments.ForEach(s =&gt;  
   {  
     s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;  
     context.FeedComments.Add(s);  
     context.SaveChanges();  
   });  
  
   var Events = new List&lt;Event&gt; {   
     new Event{   
       Begin=Convert.ToDateTime("2013-04-24 17:47:03"),  
       End=Convert.ToDateTime("2013-04-24 17:47:03"), UserSMAId=ccv  
     },  
   };  
  
   Events.ForEach(s =&gt;  
   {  
     s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;  
     context.Events.Add(s);  
     context.SaveChanges();  
   }); 
  </code></pre>
  
  <p>This is my code for each entity:</p>
  
  <pre><code>public partial class FeedType : BaseEntity
  {
      public string Libel { get; set; }
  
      public virtual List&lt;Feed&gt; Feeds { get; set; }
  
      public FeedType()
      {
          Feeds = new List&lt;Feed&gt;();
      }
  }
  
  public partial class Feed : BaseEntity
  {
      public string Libel { get; set; }
      public string Content { get; set; }
      public DateTime CreatedDate { get; set; }
  
      [ForeignKey("UserSMAId")]
      public virtual UserSMA UserSMA { get; set; }
      public Int64 UserSMAId { get; set; }
  
      [ForeignKey("FeedTypeId")]
      public virtual FeedType FeedType { get; set; }
      public Int64 FeedTypeId { get; set; }
  
      [ForeignKey("ImageId")]
      public virtual Image Image { get; set; }
      public Int64 ImageId { get; set; }
  
      public virtual ICollection&lt;FeedComment&gt; FeedComments { get; set;        }
  
      public Feed()
      {
          FeedComments = new List&lt;FeedComment&gt;();
      }
  }
  
  public partial class FeedComment : BaseEntity
  {
      public string Content { get; set; }
      public DateTime CreatedDate { get; set; }
  
      [ForeignKey("FeedId")]
      public Feed Feed { get; set; }
      public Int64 FeedId { get; set; }
  
      [ForeignKey("UserSMAId")]
      public UserSMA UserSMA { get; set; }
      public Int64 UserSMAId { get; set; }
  }
  
  public partial class Event : Feed
  {
      public DateTime Begin { get; set; }
      public DateTime End { get; set; }
  
      // SMA.Entities.Models.Event.UserSMAId hides 
      [ForeignKey("UserSMAId")]
      public virtual UserSMA UserSMA { get; set; } 
      // inherited member 'SMA.Entities.Models.Feed.UserSMAId
      public Int64 UserSMAId { get; set; } //Use the new keyword if hiding was intended
  }
  </code></pre>
  
  <p>Have you please any idea about the problem? How can I present the relation betwwen the Event and Feed Classes?</p>
  
  <p><strong>Update</strong></p>
  
  <p><code>SMAContext.cs</code> file:</p>
  
  <pre><code>public DbSet&lt;Event&gt; Events { get; set; }
  public DbSet&lt;Feed&gt; Feeds { get; set; }
  public DbSet&lt;FeedComment&gt; FeedComments { get; set; }
  public DbSet&lt;FeedType&gt; FeedTypes { get; set; }
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;Feed&gt;()
          .Map(m =&gt; m.ToTable("Feeds"))
          .Map&lt;Event&gt;(m =&gt; m.ToTable("Events"));
  
  }
  </code></pre>