Solved

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

Posted on 2013-01-12
1
1,631 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

10 Experts available now in Live!

Get 1:1 Help Now