gpt4 book ai didi

c# - 运行种子方法转换为值类型 失败,因为物化值为 null

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:13 24 4
gpt4 key购买 nike

我修改了一个实体以将属性从 int? 更改为 int 导致以下迁移

public override void Up()
{
AlterColumn("dbo.ChangeLogs", "ChangeGroupId", c => c.Int(nullable: false));
}

public override void Down()
{
AlterColumn("dbo.ChangeLogs", "ChangeGroupId", c => c.Int());
}

看起来我可以运行 Update-Database,但是 Seed 方法似乎失败了。我不确定 EF 正在运行哪个种子方法,但我的自定义 Initialiser 类和 EF Code First Migrations Configuration Seed 方法都没有任何实现细节

PM> Update-Database -Verbose
Using StartUp project 'PN.Helix.Web'.
Using NuGet project 'PN.Helix.Domain'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'aspnet-PN.Helix.Web-20161223010201' (DataSource: (LocalDb)\MSSQLLocalDB, Provider: System.Data.SqlClient, Origin: Configuration).
Applying explicit migrations: [201612252335030_NullableChangeGroupIdInChangeLogs].
Applying explicit migration: 201612252335030_NullableChangeGroupIdInChangeLogs.
ALTER TABLE [dbo].[ChangeLogs] ALTER COLUMN [ChangeGroupId] [int] NOT NULL
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201612252335030_NullableChangeGroupIdInChangeLogs', N'PN.Helix.Domain.Migrations.Configuration', 0x1F8B0800000000000400ED5C5B6FE3B8157E2FD0FF20E8A92D66AD24F3D20DEC5D649CC936682E83D8BBDBB7052D310EB112A595A8AC8DA2BF6C1FFA93FA177AA82B2FBA50F26D66100C30B029F2E3E1E147F21CEA73FEF7C77FA7DF6F02DF7AC57142423AB3CF2767B685A91B7A84AE6776CA9EBFF9BBFDFD777FFED3F4A3176CAC9FCA7AEF793D68499399FDC25874E93889FB8203944C02E2C661123EB3891B060EF242E7E2ECEC5BE7FCDCC100610396654D9F52CA4880B32FF0751E5217472C45FE7DE8613F29CAE1C92243B51E50809308B978667F7A98FC03FB6433B90E0344E8E4FAEACEB6AE7C82C09405F69F6D0B511A32C4C0D0CB1F13BC607148D78B080A90BFDC4618EA3D233FC1C5002EEBEAA66339BBE06371EA8625949B260CAC1A0678FEBE708EA3361FE562BB721EB8EF23B8996DF9A83317CEECF90BA26B7C17AE6D4BEDED72EEC7BCA6EEE27C522655DB779652E35DC50C2010FFF7CE9AA73E4B633CA3386531F2A149BAF289FB4FBC5D86BF623AA3A9EF8BA682B1F04C2A80A24F7118E1986D9FF07331805BCFB61CB99DA336AC9A096DF291DD52F6FEC2B61EA073B4F271C504C10B0B16C6F8074C718C18F63E21C6704C3906CE7CA9F5AEF4F52926018AB760D24FC84F71D931B01056946DDDA3CD1DA66BF632B3E1A36DDD900DF6CA92C2981F298105088D58CC01BAFBCB67987F3E7857E5D7A374F6E87BC771E003FEFD381D5D039FF22554D191172D09F7A6C6C86EAC1CE787384CA37E728B5053A7DE123A378AC50BDFA2E97A89925F47EC1562F3B7EDA2BDAFCC4F9C063B532243CA67AB20321C8613A17420DE559260C691243CA174A87DE9EA2E7433A7DDC461308CB69D70CB7030D8037A25EBAC71B795B6F584FDEC5BF24222D1A705B77FD11AF0FF9F425F59426ABD5F16611ABBDCB7A141E5258AD7988D1AC5321C34065EBD7F04CBD0D87EA8DA64BDF93E54438DD986EAD66FBB507B5FC7091F500CD694D3B1BF252BE336B2BD7C2812331189DE58A1226EC9F1E65AE54A1845EF1DB8FD46EC5311DB64DF4DF6494475B3EDA6EB282242A61C675C9823C83247D05106382529654BC65054473816617FC72BDE39F0EFB754E8F8609BB2CB07F881B0A4ECE903A190BE9AF5D40D7D8712F6734C0687B6ED944D033541C923DBDBE4C647EBFA5E675CB6B2D7540566C1C3B1BFE51191401DD961F73858E1B88CB71983B9B0AD2C199DD9679A7BA5DAD7188626543FEFAEFE842344E2AAF685EEF1DCB71DFE1622FF9DFD5D619DD0DFB06F8641C8C82B36F5F9CF68CD0FEA668F37FB10061ABA24738A72E0CB2780DCEB47EA5946C7419D97D571C43DB88B44E0205842DC4A75BF7CA4D7D8C70C5B576E7E453947898B3C7D39C380BC617655F192907F8AE1BB6CDADFB41E610BC73C9623C89F031ECC36A14CDFEF097549847C1317298D0D63183EF8AA1BF5C9358E30E5BBB9892B4CFAD7E362DD9AAA53658AFA3C367504067613B333AB6DE381598AAB5C4754B74A7D7C30EC6718EDF6BA224631D664304720AEC9DC9998D170B1F3591178190EA6AF78BF7138F20A57235F3A75EBA19C90B8F5AC0DA46D7E817804D2E6812DB461D0A28A2BB2F88717E20D6B08A6201F28E2A9A488E955A271D00566EA2B3E88E9EB48BA6097F0FE4FE3AB0C237AB909495E0D7D6052BAAB63899CEF81EAC231069113BC3B92B00634352556308539D6AD53127CA16EE74D804A41D348B01A9FE41F8DD1A6019C00278F43DD55641F18F8A7FBEE5C77937954323C2E11472993BDC3714691C8F1FDC7EFEDCDBDA71E89430FC5BD7A4E380677F75B797950EDB1B594C4C9B524A5E6C469119D4CEF511481998208A528B116B90265FECD62B83223C8311C3769106854D6563DB130466BAC3C85AEC1D21B1227FC95215A219E7CCFBD40AB269D282D3B61D9957668E83358EE8B6513FEB9C85D74318E70C4E8C770817003830BF8819EDD9609B3DED1D6E26220E4A3B8E13A791EFA6940DB838BF6D69A5C4384D21E9AE38AB20C11522C1F62A5A8BC904D149F9823D6F20A11AD2E3547AAF51322525D6A8E2409244430E981399E2292101195473AE6D451A8AA0587DAB2D0026E79A1192D437937DD75254A21DAF0C5D8DDFC30EB51D043882042F140AC3C3CD5B09AA2D62E2CE1F653C4128A07D8A527CC927DFDF9B411769ED5B42037A73CA7E37D77103C90F642363182F55DAD0F437A7D5B1F7C40687788D221D17BC378B289DFDFACEF30E59FEF7C9F68561AD2E35DA74749A6874F521F409BB3F577B9A2E3FBDE1577216BEF6A4560EDA139AEF04E5644148ACDB18497B02296507C64D269099A5AA5EABD4AD494846C5A2447FD3F15D0B2A5BC8A6D81935E89C733A5C536613898F00A93C56FFEDC2730DEBAC23DA2E419272C7FD3685F9C9D5F283F36F87C84FF4E92787E4372D9A8FE9727EC085A1FC2DDDA2B8E1828026D11DBD35714BB2F28FE4B80367F1541C709EA77826B12CDEF04A80AE3770253C5EF3B813508DC3D28627B13B8671C3A80BCFDEB580C9A947CB4F3352979BFE375104D3F3E06A455349E8169AF4A6EA9873733FBDF59EB4BEBF65FEA0DEDADF7CE7A8C6163BFB4CEACFFEC4B743EC218DE7C8029A3E4D25F07ADF7B713B7E88F4D674F6D7F88E97B9BBB41EEEACA094EAD153D88435BE4A13BAD0B4D020A60AB4C053A0A4E937D1A1E436D93BE2F09DD29E572D2FBED93A8E38EA2CBE8BC77FBCA846FC367F450DC927418A3E54AE3E44003677C8FC29FAF5E9AF646B02F8B60271691E9CA80969B5EED2AA8532496DF98C121BE0A61EEF3C35B14980D1091F56AC89AFAEA949774C9CCFA54668D9D75694EDA74689D32B4A65ECCBB500568BD0AB5A6EEE43A7AA7A791B1354AD6BAE543FADC6AEFEBBE1C7DDAF0C174ECC96DEFECBF0CB9D967E68A010A32FD45046CEBC25F3682F32521EB1A82FF9D238A5D6943AFEADCD2E7B03C60148BCA2A4AA6758F1982E40A5DC58C3C2397C163172749F643C1E2474A1F8315F66EE963CAA294F11BB960E54BBF41E4E75357FF994C4EB679FA1865AB7C1F43003309CF0F1FE98794F85E65F74D437ED802C10FBE22C3E673C978A6BDDE56480F2135042ADC579DD74B1C443E80258F7481845F890DB00DF6DE3BBC46EEB67C9FD40ED23F11B2DBA7D704AD6314240546DD1EBE0287BD60F3DDFF01D076358EE04B0000 , N'6.1.3-40302')

Running Seed method.
System.InvalidOperationException: The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.Max[TSource,TResult](IQueryable`1 source, Expression`1 selector)
at PN.Helix.Domain.DAL.HelixContext.SaveChanges() in C:\Users\Administrator\Documents\GitHub\PacificNational\PN.Helix\PN.Helix.Domain\DAL\HelixContext.cs:line 58
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

自定义初始化程序:

public class HelixInitialiser : DropCreateDatabaseIfModelChanges<HelixContext>
{
protected override void Seed(HelixContext context) { }
}

迁移配置:

internal sealed class Configuration : DbMigrationsConfiguration<PN.Helix.Domain.DAL.HelixContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}

protected override void Seed(PN.Helix.Domain.DAL.HelixContext context) { }
}

大多数情况下,当这种情况发生在 SO 上时,人们正在使用 LINQ 查询。那么为什么这会发生在一个空的种子方法中呢?

最佳答案

我想更仔细地检查您的堆栈跟踪是值得的。我覆盖了 DbContext.SaveChanges() 方法,该方法在其中运行 Linq 查询。

at System.Linq.Queryable.Max[TSource,TResult](IQueryable`1 source, Expression`1 selector)
at PN.Helix.Domain.DAL.HelixContext.SaveChanges() in C:\Users\Administrator\Documents\GitHub\PacificNational\PN.Helix\PN.Helix.Domain\DAL\HelixContext.cs:line 58
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()

由于数据库中没有记录,Linq 查询失败。

关于c# - 运行种子方法转换为值类型 <type> 失败,因为物化值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com