Solved

entity framework can't insert null error on generated id column

Posted on 2013-01-12
1
1,643 Views
Last Modified: 2013-01-18
I have a strange issue where I started getting an error trying to insert into a table.  

Cannot insert the value NULL into column 'Id', table 'OptionSettlementValues'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Here is the code for that object
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public string UnderlyingSymbol { get; set; }
        public DateTime Expiration { get; set; }
        public decimal SettlementPriceUnderlying { get; set; }
        public string Type { get; set; }

        public OptionSettlementValue()
        {}

        public OptionSettlementValue(string underlyingSymbol, string expiration, string settlementPriceUnderlying, string type)
        {
            UnderlyingSymbol = underlyingSymbol;
            Expiration = DateTime.Parse(expiration);
            SettlementPriceUnderlying = decimal.Parse(settlementPriceUnderlying);
            Type = type;
        }


Here is my code to insert:
var db = new Context("JTradeAdmin");
db.OptionSettlementValue.Add(new OptionSettlementValue("OEX", "1/4/2013", "664.69", "weekly"));
db.SaveChanges();
0
Comment
Question by:jackjohnson44
1 Comment
 
LVL 25

Accepted Solution

by:
TempDBA earned 500 total points
ID: 38771096
You need to see in your table structure, if any constraint is defined for ID column which is of type guid, that by default, any value is defined to be populated into it or not. I think there is no default value and it needs you to pass it from your program using some function that populates GUID values.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
need help in sql 4 65
datetime in sql 6 24
How can i get data when i use where clause with group by? 3 20
SQL Server merge records in one table 2 11
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now