我有以下简单的实体:
public class Something{ [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int ID { get; set; } public string NAME { get; set; } public int STATUS { get; set; } }
如您所见,我不希望从数据库生成ID,但我要手动输入.这是我的DbContext类:
public class MyCEContext : DbContext { ... public DbSet<Something> Somethings { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { string dbsch = "myce"; modelBuilder.Entity<Something>().ToTable("SOMETHING",dbsch); } }
这里没什么特别的.但是这段代码失败了:
using (MyCEContext ctx = new MyCEContext()) { Something t = new Something(); t.ID= 1; t.NAME = "TEST"; t.STATUS = 100; ctx.Somethings.Add(t); ctx.SaveChanges(); }
这是错误:
指定的值不是’Edm.Decimal’类型的实例
通常,总是EF尝试将值发送到int主键字段,我得到edm.decimal错误.
有帮助吗?
正如我在之前的回答中评论的那样,我找到了更好的解决方案,这很奇怪,但它确实有效
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<TestEntity>().ToTable("TESTENTITY","SCHEMENAME"); modelBuilder.Entity<TestEntity>().Property(p => p.Id).HasColumnName("ID").HasColumnType("INT"); modelBuilder.Entity<TestEntity>().Property(p => p.TestDateTime).HasColumnName("TESTDATETIME"); modelBuilder.Entity<TestEntity>().Property(p => p.TestFloat).HasColumnName("TESTFLOAT"); modelBuilder.Entity<TestEntity>().Property(p => p.TestInt).HasColumnName("TESTINT"); modelBuilder.Entity<TestEntity>().Property(p => p.TestString).HasColumnName("TESTSTRING"); }
和TestEntity看起来像这样
public class TestEntity { public int Id{ get; set; } public string TestString { get; set; } public int TestInt { get; set; } public float TestFloat { get; set; } public DateTime TestDateTime { get; set; } }