C# 输入字符串的格式不正确EF6
我目前正在使用EF6,并尝试首先使用代码实现一对多关系:C# 输入字符串的格式不正确EF6,c#,mysql,database,entity-framework,string-formatting,C#,Mysql,Database,Entity Framework,String Formatting,我目前正在使用EF6,并尝试首先使用代码实现一对多关系: public class Schedule { public int ScheduleId { get; set; } } public class Instruction { public int Id { get; set; } public Schedule Schedule { get; set; } } 我遵守第一公约 在我的方法中,我首先尝试添加一个空计划: public class Schedul
public class Schedule
{
public int ScheduleId { get; set; }
}
public class Instruction
{
public int Id { get; set; }
public Schedule Schedule { get; set; }
}
我遵守第一公约
在我的方法中,我首先尝试添加一个空计划:
public class ScheduleRepo {
public void SetSchedule()
{
Schedule schedule = new Schedule();
using (UptrenderDbContext ctx = new UptrenderDbContext())
{
ctx.Schedules.Add(schedule);
ctx.SaveChanges();
}
}
}´
从测试调用该方法:
[TestFixture]
public class ScheduleRepoTest {
private readonly ScheduleRepo repo = new ScheduleRepo();
[Test]
public void TestSetSchedule()
{
repo.SetSchedule();
}
}
我的上下文如下所示:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class UptrenderDbContext : DbContext
{
public DbSet<Schedule> Schedules { get; set; }
public DbSet<Instruction> Instructions { get; set; } //Not used yet
public UptrenderDbContext () {
Database.SetInitializer(new DropCreateDatabaseAlways<UptrenderDbContext >());
}
}
我注意到,如果我从指令
中删除public Schedule Schedule{get;set;}
,则不会引发异常,并且Schedule
已成功添加到数据库中
我可能错过了一些非常琐碎的东西,因为我是EF6新手。堆栈跟踪会有所帮助。不清楚异常发生在哪里(您的代码,EF代码?)。
scheduleEntity
看起来像什么?@MarkC它只包含一个Id@GertArnold添加堆栈跟踪您的示例中是否有输入错误?您可以将新设置为“schedule”,但可以将“scheduleEntity”添加到数据库集中。。不管怎样,错误是当它试图创建数据库时,因此您的迁移配置似乎有问题,可能是试图填充默认记录?给定一个现有的数据库,插入这样一个简单的实体应该不会有任何问题。我会先看代码配置。
System.FormatException : Input string was not in a correct format.
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToDouble(String value)
at MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op)
at MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(IEnumerable`1 migrationOperations, String providerManifestToken)
at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
at System.Data.Entity.Database.Create(DatabaseExistenceState existenceState)
at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at Data.Repos.Impl.ScheduleRepo.SetSchedule() in C:\Users\Benjamin\Desktop\uptrender\Data\Repos\Impl\ScheduleRepo.cs:line 20
at Data.Tests.ScheduleRepoTest.TestSetSchedule() in C:\Users\Benjamin\Desktop\uptrender\Data\Tests\ScheduleRepoTest.cs:line 21