输入的字符串格式不正确EF6

时间:2018-11-06 19:35:20

标签: 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; }
}

我遵守惯例1 here

在我的方法中,我首先尝试添加一个空时间表:

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 >());
        }
    }

运行程序时,我得到了堆栈跟踪:

堆栈跟踪:

    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

我注意到,如果我从public Schedule Schedule{ get; set; }中删除Instruction,则不会引发异常,并且schedule已成功添加到数据库中。

我可能在这里错过了一些琐碎的事情,因为我是EF6的新手。

0 个答案:

没有答案