我首先使用EF6代码将我的模型推送到数据库。在创建数据库方面,一切都运行良好,它确实按预期执行种子命令。问题是第一次运行update-database命令时放在我的数据库中的一些种子是不正确的。随后的update-database运行将正确地将种子更新为其预期值。
我做了以下事情:
任何建议?
更新
这是我的模特:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using CustomerPortal.Website.Models.Enums;
namespace CustomerPortal.Website.Models
{
[Table("Document")]
public class Document
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DocumentUID { get; set; }
[ForeignKey("DocumentType")]
[Required]
public int DocumentTypeUID { get; set; }
public virtual DocumentType DocumentType { get; set; }
[Required]
[StringLength(36)]
public string DocumentToken { get; set; }
[Required]
[StringLength(255)]
public string DocumentName { get; set; }
[StringLength(50)]
public string FormNumber { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public bool SendToParticipant { get; set; }
[Required]
public bool RequiresTransformation { get; set; }
[NotMapped]
public DocumentTypeCode DocumentTypeCode
{
get { return (DocumentTypeCode)DocumentTypeUID; }
set { DocumentTypeUID = (int)value; }
}
}
}
这是我的DBMigrationsConfiguration
using System.Collections.Generic;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.SqlServer;
using System.Data.Entity.Migrations;
using CustomerPortal.Website.DAL;
namespace CustomerPortal.Website.Migrations
{
internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
SetSqlGenerator("System.Data.SqlClient", new CustomSqlServerMigrationSqlGenerator());
}
internal class CustomSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator
{
protected override void Generate(AddColumnOperation addColumnOperation)
{
SetCreatedUtcColumn(addColumnOperation.Column);
base.Generate(addColumnOperation);
}
protected override void Generate(CreateTableOperation createTableOperation)
{
SetCreatedUtcColumn(createTableOperation.Columns);
base.Generate(createTableOperation);
}
private static void SetCreatedUtcColumn(IEnumerable<ColumnModel> columns)
{
foreach (var columnModel in columns)
{
SetCreatedUtcColumn(columnModel);
}
}
private static void SetCreatedUtcColumn(PropertyModel column)
{
if (column.Name == "CreatedOnDate" || column.Name == "UpdatedOnDate")
{
column.DefaultValueSql = "GETUTCDATE()";
}
}
}
protected override void Seed(ApplicationDbContext context)
{
SeedProcess.SeedDatabase(context);
}
}
}
这是我的SeedProcess类:
using CustomerPortal.Website.DAL.Seeds;
namespace CustomerPortal.Website.DAL
{
public class SeedProcess
{
public static void SeedDatabase(ApplicationDbContext context)
{
DocumentTypeSeeds.Seed(context);
SiteImageTypeSeeds.Seed(context);
SubscriptionTypeSeeds.Seed(context);
DocumentEntityTypeSeeds.Seed(context);
StateTypeSeeds.Seed(context);
PaymentTransactionTypeSeeds.Seed(context);
PayerTypeSeeds.Seed(context);
BenefitPayoutTypeSeeds.Seed(context);
BillingFrequencyTypeSeeds.Seed(context);
CriteriaExpressionTypeSeeds.Seed(context);
FamilyCoverageTypeSeeds.Seed(context);
InstitutionTypeSeeds.Seed(context);
PaymentTypeSeeds.Seed(context);
ProductEnrollmentTypeSeeds.Seed(context);
ProductTypeSeeds.Seed(context);
ProductRateTypeSeeds.Seed(context);
BenefitTypeSeeds.Seed(context);
PotentialEnrolleeStatusTypeSeeds.Seed(context);
//Image and Document Seeds
SiteImageSeeds.Seed(context);
DocumentSeeds.Seed(context);
//Base Product & Setup Seeds
InstitutionSeeds.Seed(context);
ProductSeeds.Seed(context);
BenefitSeeds.Seed(context);
ProductBenefitSeeds.Seed(context);
OfferSeeds.Seed(context);
PlanSeeds.Seed(context);
PlanProductBenefitSeeds.Seed(context);
PlanCoverageSeeds.Seed(context);
PlanCoverageRateSeeds.Seed(context);
CriteriaGroupSeeds.Seed(context);
CriteriaSeeds.Seed(context);
CriteriaGroupCriteriaSeeds.Seed(context);
RoleSeeds.Seed(context);
UserSeeds.Seed(context);
//Customer Specific Offer Seeds
FormGroupSeeds.Seed(context);
FormGroupFormSeeds.Seed(context);
InstitutionOfferGroupSeeds.Seed(context);
InstitutionOfferGroupOfferSeeds.Seed(context);
InstitutionOfferGroupPaymentOptionSeeds.Seed(context);
//Seed Document Relations
InstitutionDocumentSeeds.Seed(context);
InstitutionOfferGroupDocumentSeeds.Seed(context);
PlanCoverageDocumentSeeds.Seed(context);
ProductDocumentSeeds.Seed(context);
ProductStateDocumentSeeds.Seed(context);
ProductMarketableStateSeeds.Seed(context);
}
}
}
最后,这里是没有正确播种的DocumentSeed类:
using CustomerPortal.Website.DAL;
using CustomerPortal.Website.Models;
using System.Data.Entity.Migrations;
namespace CustomerPortal.Website.DAL.Seeds
{
public static class DocumentSeeds
{
public static void Seed(ApplicationDbContext context)
{
context.Documents.AddOrUpdate(
new Document
{
DocumentUID = 1,
DocumentTypeUID = 2,
DocumentToken = "834cc814-1321-4ef3-8bcd-5de0c66b71e7",
DocumentName = "Crescent Medical Setup Doc.docx",
FormNumber = "N/A",
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
},
//This is the problem seed.
//DocumentUID 2 should be the welcome letter
//But on intial seed, its a privacy policy, which is actually Document UID 39
new Document
{
DocumentUID = 2,
DocumentTypeUID = 3,
DocumentToken = "af9b59e3-0270-45f4-ae7c-eb03602fc049",
DocumentName = "Crescent Medical Welcome Letter",
FormNumber = "N/A",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 3,
DocumentTypeUID = 4,
DocumentToken = "2f7f6ba0-c9ee-47f0-af6c-7672a536e7e1",
DocumentName = "AD&D AME Voluntary DOC Plan 1 Family 99078299.doc",
FormNumber = "DOC-99078299-1F",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 4,
DocumentTypeUID = 4,
DocumentToken = "d00b8f58-2229-4206-a759-06eb764f493f",
DocumentName = "AD&D AME Voluntary DOC Plan 1 Single + 1 99078299.doc",
FormNumber = "DOC-99078299-1 S1",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 5,
DocumentTypeUID = 4,
DocumentToken = "17e346c9-0abd-4d14-bb21-124fe15649e7",
DocumentName = "AD&D AME Voluntary DOC Plan 1 Single 99078299.doc",
FormNumber = "DOC-99078299-1S",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 6,
DocumentTypeUID = 4,
DocumentToken = "29c0391a-f657-49b1-9692-842d97dc1517",
DocumentName = "AD&D AME Voluntary DOC Plan 2 Family 99078299.doc",
FormNumber = "DOC-99078299-2F",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 7,
DocumentTypeUID = 4,
DocumentToken = "1cfd5698-9b23-48e5-9096-30d007a395f6",
DocumentName = "AD&D AME Voluntary DOC Plan 2 Single + 1 99078299.doc",
FormNumber = "DOC-99078299-2 S1",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 8,
DocumentTypeUID = 4,
DocumentToken = "b46f2716-aed9-4096-950f-6a7374bc7961",
DocumentName = "AD&D AME Voluntary DOC Plan 2 Single 99078299.doc",
FormNumber = "DOC-99078299-2S",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 9,
DocumentTypeUID = 4,
DocumentToken = "815d0259-636a-4d1f-9d8e-6a55e2fb3463",
DocumentName = "AD&D AME Voluntary DOC Plan 3 Family 99078299.doc",
FormNumber = "DOC-99078299-3F",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 10,
DocumentTypeUID = 4,
DocumentToken = "80a0c5c9-83fb-4f38-a3f2-349d174b58e6",
DocumentName = "AD&D AME Voluntary DOC Plan 3 Single + 1 99078299.doc",
FormNumber = "DOC-99078299-3 S1",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 11,
DocumentTypeUID = 4,
DocumentToken = "427a74d6-9df8-4e3a-b84b-29c006af2203",
DocumentName = "AD&D AME Voluntary DOC Plan 3 Single 99078299.doc",
FormNumber = "DOC-99078299-3S",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 12,
DocumentTypeUID = 4,
DocumentToken = "6323d281-8f1b-4bd0-9ac3-7c624fb8046a",
DocumentName = "AD&D AME Voluntary DOC Plan 4 Family 99078299.doc",
FormNumber = "DOC-99078299-4F",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 13,
DocumentTypeUID = 4,
DocumentToken = "eb51b5e5-2cb1-4c99-96f1-c709120c3cb4",
DocumentName = "AD&D AME Voluntary DOC Plan 4 Single + 1 99078299.doc",
FormNumber = "DOC-99078299-4 S1",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 14,
DocumentTypeUID = 4,
DocumentToken = "0a390297-e77e-44b9-bb8a-ae39f25129f0",
DocumentName = "AD&D AME Voluntary DOC Plan 4 Single 99078299.doc",
FormNumber = "DOC-99078299-4S",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 15,
DocumentTypeUID = 4,
DocumentToken = "3ec07cc2-7228-49b3-b25f-b77450c61cd7",
DocumentName = "ASHIP DOC Plan 1 Family 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 16,
DocumentTypeUID = 4,
DocumentToken = "dfb57cd5-ba6a-43d0-b66e-bd22b5cca595",
DocumentName = "ASHIP DOC Plan 1 Single + Child 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 17,
DocumentTypeUID = 4,
DocumentToken = "be9260b6-8191-4a25-bda3-0d5a39049714",
DocumentName = "ASHIP DOC Plan 1 Single + Spouse 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 18,
DocumentTypeUID = 4,
DocumentToken = "c5d68d60-a0c0-4dd4-9d7b-8beeaa92b195",
DocumentName = "ASHIP DOC Plan 1 Single 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 19,
DocumentTypeUID = 4,
DocumentToken = "b83d2f96-863a-4ab4-a665-f239b2dcf927",
DocumentName = "ASHIP DOC Plan 2 Family 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 20,
DocumentTypeUID = 4,
DocumentToken = "0608d614-fa94-40bb-9d02-13d77aa9d116",
DocumentName = "ASHIP DOC Plan 2 Single + Child 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 21,
DocumentTypeUID = 4,
DocumentToken = "84022349-3f83-414e-8636-dfcad5b68b2e",
DocumentName = "ASHIP DOC Plan 2 Single + Spouse 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 22,
DocumentTypeUID = 4,
DocumentToken = "85169edb-b2b7-4633-9036-4090daabd924",
DocumentName = "ASHIP DOC Plan 2 Single 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 23,
DocumentTypeUID = 4,
DocumentToken = "c4b9cfd6-23db-4727-a05c-d8e3b897b28e",
DocumentName = "ASHIP DOC Plan 3 Family 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 24,
DocumentTypeUID = 4,
DocumentToken = "02de6cb2-0db4-4b90-8a6c-c87a1d5648ef",
DocumentName = "ASHIP DOC Plan 3 Single + Child 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 25,
DocumentTypeUID = 4,
DocumentToken = "87710917-e9b2-45e9-be24-dfa1ebbebc9c",
DocumentName = "ASHIP DOC Plan 3 Single + Spouse 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 26,
DocumentTypeUID = 4,
DocumentToken = "aac423ea-8c22-401c-96b2-9b241f34c87c",
DocumentName = "ASHIP DOC Plan 3 Single 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 27,
DocumentTypeUID = 4,
DocumentToken = "beb27c9f-2c1f-4b30-a1bc-f5e0c76941a1",
DocumentName = "ASHIP DOC Plan 4 Family 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 28,
DocumentTypeUID = 4,
DocumentToken = "1fff7ab9-34a3-420b-96af-1872a14c464b",
DocumentName = "ASHIP DOC Plan 4 Single + Child 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 29,
DocumentTypeUID = 4,
DocumentToken = "c9f87921-72a1-4cd5-91a5-91942da063fd",
DocumentName = "ASHIP DOC Plan 4 Single + Spouse 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 30,
DocumentTypeUID = 4,
DocumentToken = "0ed44913-35b6-4233-9367-bd023fcac0a2",
DocumentName = "ASHIP DOC Plan 4 Single 99078298.doc",
FormNumber = "ASHIP5000-DOC RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 31,
DocumentTypeUID = 4,
DocumentToken = "e75b63fd-1a7e-40e7-9c0e-482684668f12",
DocumentName = "CI DOC Plan 1 Primary + Sp 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 32,
DocumentTypeUID = 4,
DocumentToken = "407a0587-3b06-4a7a-a55f-b354b5f41e3b",
DocumentName = "CI DOC Plan 1 Primary 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 33,
DocumentTypeUID = 4,
DocumentToken = "59928258-c18b-4421-ac64-a37b484405e5",
DocumentName = "CI DOC Plan 2 Primary + Sp 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 34,
DocumentTypeUID = 4,
DocumentToken = "9782f1a3-2f44-4d01-b2f8-25e894afaf5a",
DocumentName = "CI DOC Plan 2 Primary 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 35,
DocumentTypeUID = 4,
DocumentToken = "20ccd21b-2ee4-421e-bbe3-c38d60802400",
DocumentName = "CI DOC Plan 3 Primary + Sp 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 36,
DocumentTypeUID = 4,
DocumentToken = "e72db228-fc77-4d19-b681-e3e0a0ad3a95",
DocumentName = "CI DOC Plan 3 Primary 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 37,
DocumentTypeUID = 4,
DocumentToken = "0d69aedf-53e2-4105-a6bb-3a976d8bf366",
DocumentName = "CI DOC Plan 4 Primary + Sp 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 38,
DocumentTypeUID = 4,
DocumentToken = "f5e3a49c-2eb6-425d-91ee-ed8e5ed29b89",
DocumentName = "CI DOC Plan 4 Primary 99078320.docx",
FormNumber = "Form 44-10-0672 (Ed. 03-14) RI",
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 39,
DocumentTypeUID = 1,
DocumentToken = "33274185-c673-44ed-b8e5-2a10b6d34263",
DocumentName = "Crescent Medical Bridge Privacy Policy.docx",
FormNumber = null,
IsActive = true,
SendToParticipant = true,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 40,
DocumentTypeUID = 7,
DocumentToken = "ab2395f5-dbe5-405c-9bc4-c0e90166e55f",
DocumentName = "AD&D AME Basic Policy 99078426.pdf",
FormNumber = null,
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 41,
DocumentTypeUID = 7,
DocumentToken = "e2a59a6e-50e9-4dcd-9e40-8ced64a30568",
DocumentName = "AD&D AME Voluntary Policy 99078299.pdf",
FormNumber = null,
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 42,
DocumentTypeUID = 7,
DocumentToken = "35f7384f-d268-4762-af20-1194e9b95213",
DocumentName = "ASHIP Policy Blanket 99078349.pdf",
FormNumber = null,
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 43,
DocumentTypeUID = 7,
DocumentToken = "5c60fcfd-1b78-47e4-b5af-f2e4f15ed95d",
DocumentName = "ASHIP Voluntary Policy 99078298.pdf",
FormNumber = null,
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 44,
DocumentTypeUID = 7,
DocumentToken = "caca3175-06f0-4e40-8c7a-e0e1ba937051",
DocumentName = "CI Policy 99078337 embedded.pdf",
FormNumber = null,
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
}
,
new Document
{
DocumentUID = 45,
DocumentTypeUID = 7,
DocumentToken = "e66f05b0-61e3-4662-885d-576993a9ed6f",
DocumentName = "CI Vol Policy 99078320.pdf",
FormNumber = null,
IsActive = true,
SendToParticipant = false,
RequiresTransformation = false
}
);
}
}
}
数据库中的初始种子记录:
后续updata-database命令后的种子数据:
更新
我将以下代码添加到SeedDatabase方法的顶部:
if (System.Diagnostics.Debugger.IsAttached == false)
System.Diagnostics.Debugger.Launch();
这允许我调试在update-database命令期间调用的种子方法。
潜在的问题似乎是,在某些时候,对我的模型中的数据进行了快照,并将其存储在代码中的某个位置。在最初调用种子方法之前,数据的快照已经在上下文中。
我正在使用AddOrUpdate扩展,出于某种原因,即使对象与快照数据不同,种子模型数据也不会在第一次调用时更改。
无论如何,解决方法是简单地清除种子方法顶部的Documents表。这显然只是将记录设置为我想要的记录,所以现在解决了问题。