CreateDateTime datetime 未检查 UpdateDateTime datetime 未检查 UpdatedByOID uniqueidentifier 已检查 AdminAbbr varchar(4) 已检查 未选中 此外,OID也设置为主键。 再次感谢, 解决方案 我无法猜测它试图将PK插入为null的任何场景,因为你的OID类型不能为空。 我想,默认情况下StoreGenerationPattern是Identity,所以当它创建表时设置Identity PK列。除非你配置为None。 检查是否创建了Identity PK列? 如果不这样做 protected override void OnModelCreating(DbModelBuilder modelBuilder) { ; modelBuilder.Entity< tAdmin>()。Property(s => s.OID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); } 希望这有助于您...... Please help,I am burning way too much time on something that should be so easy. The following code does not work and throws an excepiton that I cannot insert null into the OID field. In debug mode I can see a fully populated tAdmin entity including a value for the OID field. I know the context below hits the intended database because the same context is used to retrieve the records from the database. Totally frustrated. Thanks to anyone who attempts to help me. publicvoid Add(tAdminentity) {DomainModel.Entities.Entities db = new DomainModel.Entities.Entities(); //context.tAdmins.Add(admin);db.tAdmins.Add(entity);db.SaveChanges(); //throw new NotImplementedException();}---------Here is the tAdmin class. publicpartialclasstAdmin{ publictAdmin(){} // Primitive properties[Key]publiclong OID { get;set; }publicint ApplicationTypeOID { get;set; }publicint AppNoRangeOID { get;set; }publiclong PlanPricingOID { get;set; }publicstring ReportDocumentClass { get;set; }etc..}table def follows:OID bigint UncheckedApplicationTypeOID int UncheckedObjectTypeOID UncheckedAppNoRangeOID int UncheckedPlanPricingOID bigint UncheckedReportDocumentClass nvarchar(255) Uncheckedgl_sys_company_id int UncheckedTheme nvarchar(50) UncheckedMasterPage nvarchar(50) UncheckedDomainName nvarchar(50) UncheckedCreateDateTime datetime UncheckedUpdateDateTime datetime UncheckedUpdatedByOID uniqueidentifier CheckedAdminAbbr varchar(4) Checked UncheckedAlso OID is set as Primary Key. Thanks again, 解决方案 Hi,I couldn't guess any of the scenario it tries to insert PK as null, as your OID type is not nullable long.I guess, by default StoreGenerationPattern is Identity, so when it creates table sets Identity PK column. unless you configure to None.Check is it created Identity PK column?If not do this protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<tAdmin>().Property(s => s.OID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); }Hope this helps you.... 这篇关于不插入新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 00:12