不插入新实体 [英] Not Inserting New Entity

查看:48
本文介绍了不插入新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,


我在太容易上的时间上花费太多时间。 以下代码不起作用,并抛出一个我无法在OID字段中插入null的例外。 在调试模式下,我可以看到一个完全填充的tAdmin实体,其中包含OID字段的值
。 我知道下面的上下文命中了目标数据库,因为使用相同的上下文从数据库中检索记录。 完全沮丧。 感谢任何试图帮助我的人。



 



public
void
添加(
tAdmin


entity)


{



DomainModel.Entities。






实体
db =
new
DomainModel.Entities。
实体


();


 








// context.tAdmins.Add(admin);







db.tAdmins.Add(entity);


db.SaveChanges();



 





抛出新的NotImplementedException();







}



---------


这是tAdmin类。



 



public
partial
class
tAdmin


{


 






public


tAdmin()

{





}


 



 





//原始属性







[



]

public
long
OID {
get ;
set ;
}

public
int
ApplicationTypeOID {
get ;
set ;
}

public
int
AppNoRangeOID {
get ;
set ;
}

public
long
PlanPricingOID {
get ;
set ;
}

public
string
ReportDocumentClass {
get ;
set ;
}


等..


}


表格定义如下:


OID  bigint 未选中

ApplicationTypeOID  int 未选中

ObjectTypeOID  未选中

AppNoRangeOID  int 未检查

PlanPricingOID  bigint 未选中

ReportDocumentClass  nvarchar(255) 未检查

gl_sys_company_id  int 未检查

主题  nvarchar(50) 未检查

主页  nvarchar(50) 未选中

DomainName  nvarchar(50) 未选中
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.

 

public void Add(tAdmin

entity)

{

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.

 

public partial class tAdmin

{

 

public

tAdmin()
{

}

 

 

// Primitive properties

[

Key]
public long OID { get; set; }
public int ApplicationTypeOID { get; set; }
public int AppNoRangeOID { get; set; }
public long PlanPricingOID { get; set; }
public string ReportDocumentClass { get; set; }

etc..

}

table def follows:

OID bigint Unchecked
ApplicationTypeOID int Unchecked
ObjectTypeOID  Unchecked
AppNoRangeOID int Unchecked
PlanPricingOID bigint Unchecked
ReportDocumentClass nvarchar(255) Unchecked
gl_sys_company_id int Unchecked
Theme nvarchar(50) Unchecked
MasterPage nvarchar(50) Unchecked
DomainName nvarchar(50) Unchecked
CreateDateTime datetime Unchecked
UpdateDateTime datetime Unchecked
UpdatedByOID uniqueidentifier Checked
AdminAbbr varchar(4) Checked
  Unchecked

Also 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....


这篇关于不插入新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆