Byr*_*ahl 1 c# entity-framework-4 ef-code-first
我有以下类型:
public class Minutes
{
public double Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
此类型用于以下实体:
public class Race
{
public string Name { get; set; }
public string Location { get; set; }
public Minutes TotalMinutes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有这样的DbContext:
public class RaceDataContext: DbContext
{
public DbSet<Race> Races { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<Minutes>().Property(x => x.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
在应用程序开发中,我们一直在使用MS-SQL数据库,没有任何问题.现在,我们必须转移到客户端的Oracle数据库.Oracle数据库具有固定表,其中包含我们无法更改的列.我们需要能够阅读,更新和插入比赛.所以,我已经将我的映射代码更新为:
public class RaceDataContext: DbContext
{
public DbSet<Race> Races { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<Minutes>().Property(x => x.Value);
modelBuilder.Entity<Race>().Property(x => x.TotalMinutes).HasColumnName("RACEMINUTES"); // <-- ERROR HERE
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有编译.它表示TotalMinutes必须是一个不可为空的值,可用作.Property()方法的参数.这是构建输出的实际错误:
类型'Races.Minutes'必须是非可空值类型,以便在泛型类型或方法'System.Data.Entity.ModelConfiguration.Configuration.StructuralTypeConfiguration.Property(System.Linq.)中将其用作参数'T'. Expressions.Expression>)'C:\ Projects\Races\RaceDataContext.cs
你接近解决方案.
modelBuilder.Entity<Race>().Property(x => x.TotalMinutes.Value)
.HasColumnName("RACEMINUTES");
Run Code Online (Sandbox Code Playgroud)