|
-
Dec 13th, 2015, 03:02 PM
#1
Thread Starter
PowerPoster
[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);
}
}
}
Last edited by BrailleSchool; Dec 13th, 2015 at 03:13 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|