Solved

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

Posted on 2013-01-12
1
1,653 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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