0

This is the code I have:

Controller action method:

[HttpPost]
public ActionResult Create(Goal goal)
{
    if (ModelState.IsValid)
    {
        repository.SaveGoal(goal);
        return View("Index");
    }
    else
    {
        return View(goal);
    }
}

Model:

public class Goal
{
    [Required]
    public int GoalId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    [HiddenInput(DisplayValue = false)]
    public decimal Latitude { get; set; }

    [Required]
    [HiddenInput(DisplayValue = false)]
    public decimal Longitude { get; set; }

    [Required]
    public bool IsPrivate { get; set; }
}

Repository:

public class EFGoalRepository : IGoalRepository
{
    private EFDbContext context = new EFDbContext();

    public IQueryable<Goal> Goals
    {
        get { return context.Goals; }
    }

    public void SaveGoal(Goal goal)
    {
        context.Goals.Add(goal);
        context.SaveChanges(); // the line which causes the exception
    }
}

The problem: When I try to save a new Goal object with GoalId set to 0, I get the following error:

Cannot insert the value NULL into column 'GoalId', table 'TravelGoals.dbo.Goals'; column does not allow nulls. INSERT fails. The statement has been terminated.

I'm still new at ASP.NET MVC, but I believe this worked when I tried it a month ago with a different project.

All the values in my Goal object are valid (non-null, Name is of correct length). The GoalId property is set to 0, so it is not null either. I thought that Entity Framework would automatically assign a value to it.

What can I do to make this work?

2
  • 3
    Did you configure the ID to be a db generated column when you created the table in EF? Commented Jul 17, 2014 at 9:43
  • 1
    Apparently I didn't. I set the column to be identity in SQL Server, works as a charm now. Thank you! Commented Jul 17, 2014 at 9:47

2 Answers 2

3

What I needed to do was setting the column as identity in SQL Server.

Probably the simplest way to do this (assuming you're using Visual Studio):

  • Open the SQL Server Object Explorer
  • Double-click the table you want to edit (or right-click and select View Designer)
  • Open the Properties window
  • Select the column in the Identity Column property as shown below

Properties window showing the Identity Column property

Sign up to request clarification or add additional context in comments.

1 Comment

If you're going to answer your own question I'd suggest adding a bit more detail about where/what etc so it's useful for future readers
1

Problem is GoalId is not identity.Put this attribut on GoalId

[Key]
public int GoalId { get; set; }

If your EF version is lower than 5 use this:

[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int GoalId { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.