我目前正在使用ef6,并尝试首先使用代码实现一对多关系:
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 >());
}
}
运行程序时,我得到堆栈跟踪:
堆栈跟踪:
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新手。
暂无答案!
目前还没有任何答案,快来回答吧!