简体   繁体   中英

Why can't I insert row into Entity Framework table?

I have used the option to generate EF code from the DB. When I try to insert, I get the error that "bundle_id" is required, even though what I'm inserting has a bundle_id.

Here is my code

[Table("[storage.tblCBMinCreditRequirement]")]
public partial class storage_tblCBMinCreditRequirement
{
    [Key]
    [Column(Order = 0)]
    [StringLength(255)]
    public string bundle_id { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(255)]
    public string deal_detail_id { get; set; }

    [StringLength(10)]
    public string version_code { get; set; }

    [StringLength(255)]
    public string applicant_id { get; set; }

    [StringLength(255)]
    public string score { get; set; }

    [StringLength(255)]
    public string thin_no_hit { get; set; }

    [StringLength(255)]
    public string thin_no_hit_proxy { get; set; }

    [StringLength(255)]
    public string thin_file { get; set; }

    [StringLength(255)]
    public string thin_file_no_hit { get; set; }

    [StringLength(255)]
    public string limited_credit_flag { get; set; }

    [StringLength(255)]
    public string limited_credit_empl { get; set; }

    [StringLength(255)]
    public string limited_credit_sat_auto { get; set; }

    [StringLength(255)]
    public string qualified_trades { get; set; }

    [StringLength(255)]
    public string sat_auto_mortgage_trade { get; set; }

    [StringLength(255)]
    public string min_num_sat_auto_trade { get; set; }

    [StringLength(255)]
    public string total_bks { get; set; }

    [StringLength(255)]
    public string delinquent_mortgage { get; set; }

    [StringLength(255)]
    public string delinquent_auto { get; set; }

    [StringLength(255)]
    public string high_credit_amount { get; set; }

    [StringLength(255)]
    public string min_age_on_file { get; set; }

    [StringLength(255)]
    public string min_fico { get; set; }

    [Column(TypeName = "numeric")]
    public decimal? CBCreditScoreFactors_Id { get; set; }
}

storage_tblCBMinCreditRequirement newt = InsertTable<storage_tblCBMinCreditRequirement>(new storage_tblCBMinCreditRequirement(), "/Loan/CoApplicant/CBCreditScoreFactors/CBMinCreditRequirement");

db.storage_tblCBMinCreditRequirement.Add(newt); 
db.SaveChanges();//throws exception

Here is my DB. 数据库映像

Here is my entity 实体1 实体2

Here is my error 在此处输入图片说明

Thanks for any help, I'm stumped! :) And now, a poem for stack overflow, for being ridiculous in wanting more of a description.

Stack overflow, you should know, That code is the mode, to display my rage at the machine.

How else will I debug thee? What vain words do you want from me? Must my idle words be of use? How does thou determine the value of my words and code? Is not code enough? Does forced poetry do you justice, O' Stack Overflow?

Just to add to @DevilSuichiro's original comment you'll need to set the DatabaseGeneratedOption.None on the property bundle_id of your entity like below. The fully qualified name was provided so you don't have to hunt for the namespace :)

[Key]
[Column(Order = 0)]
[StringLength(255)]   
[DatabaseGenerated(DatabaseGeneratedOption.None)] // Located here: System.ComponentModel.DataAnnotations.Schema
public string bundle_id { get; set; }

Why?

By default DatabaseGeneratedOption.Identity is set by default in Entity Framework on a primary key column. This indicates that the Database is responsible for generating the value. As a result, Entity Framework excludes the column from any generated SQL Insert statements as it is expecting SQL to generate the value.

To see this in action, either use

yourDbContext.Database.Log() = (query) => Debug.WriteLine(query);

before you execute SaveChanges or run a SQL Profiler to see the differences in the generated SQL.

UPDATE

For clarity, when you use the Database.Log() = (query) => Debug.WriteLine(query); the results are output to the Diagnostic Tools window of Visual Studio. This is launched by default when you start a debugging session unless you previously closed the window.

If it is not showing up for you while debugging it can be found here: Debug -> Windows -> Show Diagnostic Tools.

Below is a sample output so you know what you are looking for:

诊断工具样本输出

您可以尝试在cshtml文件中为bundle_id隐藏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM