Search code examples
asp.net-mvcentity-frameworkormdatabase-first

Adding to EntitySet not working


I'm trying to add an object to a database-first ORM EntitySet in an MVC project. I use a piece of code something like this:

public static Boolean CreateListing(string title, string description)
{
    ListingEntities ce = new ListingEntities();
    ce.Ads.AddObject(new Ad()
    {
        ID = Guid.NewGuid(),
        Title = title,
        Description = description,
    });
    return ce.SaveChanges() == 1;
}

However, the SaveChanges method throws a Data.UpdateException which is thrown by a SqlClient.SqlException. The latter says

"Cannot insert the value NULL into column 'ID', table 'Listings.dbo.Ads'; column does not allow nulls. INSERT fails. The statement has been terminated."

I wholeheartedly agree. I just don't see why the ID should be null when it seems I set it immediately prior. Any suggestions?

Thanks,
Nathan


Solution

  • Someone else on my team configured the database to create its own ID's, and the issue is resolved.