Entity framework 自动性感觉发生
使用EF 5.0,并应用代码优先。 调用计数方法时发生异常 (Id是主键) 问题是,异常消息实际上引用了一个类,它不是try范围的一部分。 此消息不仅在迁移现有sdf时记录。 从头开始创建新sdf时也是如此 迁移脚本---在异常消息中提到:Entity framework 自动性感觉发生,entity-framework,code-first,Entity Framework,Code First,使用EF 5.0,并应用代码优先。 调用计数方法时发生异常 (Id是主键) 问题是,异常消息实际上引用了一个类,它不是try范围的一部分。 此消息不仅在迁移现有sdf时记录。 从头开始创建新sdf时也是如此 迁移脚本---在异常消息中提到: CreateTable( "dbo.OtherRecords", c => new
CreateTable(
"dbo.OtherRecords",
c => new
{
Id = c.String( nullable: false, maxLength: 128 ),
} )
.PrimaryKey( t => t.Id ).Index( t => t.Id );
迁移脚本---语句中使用的类(在try范围内)
如何澄清这一例外
编辑:当AutomaticMigrationDataLossAllowed=true时应用代码>,则出现上述异常。
当AutomaticMigrationDataLossAllowed=false时应用代码>,然后发生另一个异常,如下所述:
System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException
:未应用自动迁移,因为它会导致数据丢失
结果表进行了比较-没有任何数据丢失
编辑:与OtherRecords一样位于相同Up()方法中的另一个迁移代码
CreateTable(
"dbo.DTRParts",
c => new
{
Id = c.Long( nullable: false, identity: true ),
TimestampTicks = c.Long( nullable: false ),
DeviceTypeId = c.String(maxLength: 4000),
CommandCode = c.Int( nullable: false ),
RequestId = c.Long( nullable: false ),
DataType = c.Byte( nullable: false ),
NumberOfParts = c.Int( nullable: false ),
PartNumber = c.Int( nullable: false ),
IsDeflated = c.Boolean( nullable: false ),
TraceData = c.Binary( maxLength: 8000 ),
} )
.PrimaryKey( t => t.Id ).Index( t => t.Id );
有人能解释为什么这些脚本会出现自动的CDataLossException吗?(新生成sdf时也会发生此情况)
注释了Down()中的代码。因此,此时没有数据丢失。
并且根本不调用Down()
如上所述,使用了EF 5.0(在开发和生产中使用)。
EF 6或更高版本中所述的自动CDATA丢失异常(例如,对于空内容/新sdf)是否也是一个问题?如果删除.Index()方法,是否有效?PK应该是自动的indexed@Vlad274你已经尝试过了,没有-同样的例外是你唯一的迁移?是否启用了自动迁移?@Vlad274否,存在另一个迁移脚本,但没有外键或类似于迁移脚本中提到的外键。是的,是自动的。
CreateTable(
"dbo.OtherRecords",
c => new
{
Id = c.String( nullable: false, maxLength: 128 ),
} )
.PrimaryKey( t => t.Id ).Index( t => t.Id );
CreateTable(
"dbo.MyRecords",
c => new
{
Id = c.Long(nullable: false, identity: true),
SerialNumber = c.String(maxLength: 4000),
Location = c.String(maxLength: 4000),
Ticks = c.Long(nullable: false),
})
.PrimaryKey( t => t.Id ).Index( t => t.Id);
CreateTable(
"dbo.DTRParts",
c => new
{
Id = c.Long( nullable: false, identity: true ),
TimestampTicks = c.Long( nullable: false ),
DeviceTypeId = c.String(maxLength: 4000),
CommandCode = c.Int( nullable: false ),
RequestId = c.Long( nullable: false ),
DataType = c.Byte( nullable: false ),
NumberOfParts = c.Int( nullable: false ),
PartNumber = c.Int( nullable: false ),
IsDeflated = c.Boolean( nullable: false ),
TraceData = c.Binary( maxLength: 8000 ),
} )
.PrimaryKey( t => t.Id ).Index( t => t.Id );