문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top