locked
An exception occurred while initializing the database with data RRS feed

  • Question

  • User-1055402213 posted

    I am preparing a rest API with ASP.net,I started initialising the database Tables with data,but I get this error:

    An exception occurred while initializing the database

    the problem is that the data is in tables,only one table "Event",in witch I get this problem,this is my code to initialize the database:

    var FeedTypes = new List<FeedType>
                {
                new FeedType{Libel="FeedType1"},
                };
    
    
                FeedTypes.ForEach(s =>
                {
                    s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;
                    context.FeedTypes.Add(s);
                    context.SaveChanges();
    
                });
    
    
    
                var feedtypeee = context.FeedTypes.Where(s => s.Libel == "FeedType1").First().Id;
                var Feeds = new List<Feed>
                {
                new Feed{Libel="Libel1", Content="contentnotif1" , CreatedDate=Convert.ToDateTime("2013-04-24 17:47:03"), UserSMAId=ccv, FeedTypeId=feedtypeee , ImageId=context.Images.Where(i=>i.Alt=="Image 1").FirstOrDefault().Id},
    
    
                };
    
                Feeds.ForEach(s =>
                {
                    s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;
                    context.Feeds.Add(s);
                    context.SaveChanges();
    
                });
    
    
                var FeedComments = new List<FeedComment>
                {
                new FeedComment{Content="Content1", CreatedDate=Convert.ToDateTime("2013-04-24 17:47:03"), FeedId=context.Feeds.Where(s=>s.Libel=="Libel1").FirstOrDefault().Id, UserSMAId=ccv},
    
    
                };
    
                FeedComments.ForEach(s =>
                {
                    s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;
                    context.FeedComments.Add(s);
                    context.SaveChanges();
    
                });
    
    
     var Events = new List<Event>
                {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                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 =>
                {
                    s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;
                    context.Events.Add(s);
                    context.SaveChanges();
    
                });

    this is my code for each entity:

    public partial class FeedType : BaseEntity
        {
            public string Libel { get; set; }
    
            public virtual List<Feed> Feeds { get; set; }
    
            public FeedType()
            {
                Feeds = new List<Feed>();
            }
        }
    
        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<FeedComment> FeedComments { get; set; }
    
            public Feed()
            {
                FeedComments = new List<FeedComment>();
            }
        }
    
         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; }
    
            [ForeignKey("UserSMAId")]
            public virtual UserSMA UserSMA { get; set; } //SMA.Entities.Models.Event.UserSMAId hides 
                                                         //inherited member 'SMA.Entities.Models.Feed.UserSMAId
            public Int64 UserSMAId { get; set; } //Use the new keyword if hiding was intended
        }

    SMAContext.cs file:

    public DbSet<Event> Events { get; set; }
            public DbSet<Feed> Feeds { get; set; }
            public DbSet<FeedComment> FeedComments { get; set; }
            public DbSet<FeedType> FeedTypes { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Feed>()
                  .Map(m => m.ToTable("Feeds"))
                  .Map<Event>(m => m.ToTable("Events"));
    
            }

    I think that The problem is that,the Table Event that inherits from the Table Feed has the same name of foreign Key as the class mother,How can I deal with this problem(How can I affect the same foreign key of two classes in inheritance)

    thanks for Help

    Monday, September 21, 2015 3:56 AM

All replies

  • User1686483761 posted

    Hi zorra_200

    think that The problem is that,the Table Event that inherits from the Table Feed has the same name of foreign Key as the class mother,How can I deal with this problem(How can I affect the same foreign key of two classes in inheritance)

    why do you need to the Table Event that inherits from the Table Feed? you could define the Table Event.

       public partial class Event 
        {
            public DateTime Begin { get; set; }
            public DateTime End { get; set; }
             
    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<FeedComment> FeedComments { get; set; }
    [ForeignKey("UserSMAId")] public virtual UserSMA UserSMA { get; set; } //SMA.Entities.Models.Event.UserSMAId hides //inherited member 'SMA.Entities.Models.Feed.UserSMAId public Int64 UserSMAId { get; set; } //Use the new keyword if hiding was intended

    public Event()
            {
                FeedComments = new List<FeedComment>();
            } }

    Tuesday, September 22, 2015 1:51 AM