Results 1 to 2 of 2

Thread: [RESOLVED] MVC, Seeding Database, Records Duplicating.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] MVC, Seeding Database, Records Duplicating.

    Hi, all,

    I am getting the hang of MVC slowly. But, I am having an issue with my seed method. When I run the seed method, it is re-adding the records again and again. I am not sure what is going on here. Any suggestions would be appreciated.

    Code:
    namespace NotaryNet.Web.Migrations
    {
        using Models;
        using System.Collections.Generic;
        using System.Data.Entity.Migrations;
        using System.Data.Entity.Validation;
        using System.Text;
        internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = true;
                AutomaticMigrationDataLossAllowed = true;
            }
    
            // Sometimes errors when seeding are ambiguous and tell you nothing useful.
            // Catch the errors and provide the error message encountered along with the property causing the error.
            private void SaveChanges(ApplicationDbContext ctx)
            {
                try
                {
                    ctx.SaveChanges();
                }
                catch (DbEntityValidationException EntValEx)
                {
                    StringBuilder sb = new StringBuilder();
    
                    foreach (var EntValErr in EntValEx.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation.\n", EntValErr.Entry.Entity.GetType());
    
                        foreach (var ValErr in EntValErr.ValidationErrors)
                        {
                            sb.AppendFormat("{0} : {1}", ValErr.PropertyName, ValErr.ErrorMessage);
                            sb.AppendLine();
                        }
                    }
                }
            }
    
            protected override void Seed(ApplicationDbContext ctx)
            {
                // Add default options for the Company Types.
                var CompanyTypes = new List<CompanyTypes>
                {
                    new CompanyTypes { CompanyTypeDescription = "Accountant", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Customer", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Employee", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Financial Institution", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Notary Company", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Supplier", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Vendor", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Other", IsDeletable = false, IsUsed = false }
                };
    
                // Add the default options to the database table.
                CompanyTypes.ForEach(ct => ctx.CompanyTypes.AddOrUpdate(ctid => ctid.CompanyTypesID, ct));
                SaveChanges(ctx);
            }
        }
    }

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: MVC, Seeding Database, Records Duplicating.

    Okay. I figured it out and it was a stupid mistake I made. I was checking for the record ID and not the value of the record. I got it working now.

    Code:
    namespace NotaryNet.Web.Migrations
    {
        using Models;
        using System.Collections.Generic;
        using System.Data.Entity.Migrations;
        using System.Data.Entity.Validation;
        using System.Text;
        internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = true;
                AutomaticMigrationDataLossAllowed = true;
            }
    
            // Sometimes errors when seeding are ambiguous and tell you nothing useful.
            // Catch the errors and provide the error message encountered along with the property causing the error.
            private void SaveChanges(ApplicationDbContext ctx)
            {
                try
                {
                    ctx.SaveChanges();
                }
                catch (DbEntityValidationException EntValEx)
                {
                    StringBuilder sb = new StringBuilder();
    
                    foreach (var EntValErr in EntValEx.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation.\n", EntValErr.Entry.Entity.GetType());
    
                        foreach (var ValErr in EntValErr.ValidationErrors)
                        {
                            sb.AppendFormat("{0} : {1}", ValErr.PropertyName, ValErr.ErrorMessage);
                            sb.AppendLine();
                        }
                    }
                }
            }
    
            protected override void Seed(ApplicationDbContext ctx)
            {
                // Add default options for the Company Types.
                var CompanyType = new List<CompanyTypes>
                {
                    new CompanyTypes { CompanyTypeDescription = "Accountant", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Customer", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Employee", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Financial Institution", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Notary Company", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Supplier", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Vendor", IsDeletable = false, IsUsed = false },
                    new CompanyTypes { CompanyTypeDescription = "Other", IsDeletable = false, IsUsed = false }
                };
    
                // TODO: The records are being re-added each time the seed method is run, ending in duplications.
                // Add the default options to the database table.
                CompanyType.ForEach(ct => ctx.CompanyTypes.AddOrUpdate(ctd => ctd.CompanyTypeDescription, ct));
                SaveChanges(ctx);
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width