0

I'm using EF in order to insert simple object into db:

public bool AddQuestion(Question question)
{
    try
    {
        _ctx.Questions.Add(question);

        _ctx.SaveChanges();
        return true;
    }
    catch(Exception ex)
    {
        return false;
    }
}

my DbContext look like:

 public class AnyContext:DbContext
 {
    public AnyContext():base()
    {
        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = false;

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<AnyContext, AnyContextMigrationConfiguration>());
    }
    public AnyContext(string connectionString)
        : base(connectionString)
    {
         Configuration.ProxyCreationEnabled = false;
         Configuration.LazyLoadingEnabled = false;

         Database.SetInitializer(new MigrateDatabaseToLatestVersion<AnyContext, AnyContextMigrationConfiguration>());
    }
     public DbSet<Question> Questions { get; set; }
 }

And POCO look like:

[Table("Questions")]
public class Question
{
    public int Id  { get; set; }
    public String body { get; set; }
    public String img_path { get; set; }
    public int location { get; set; }
    public int status { get; set; }
    public DateTime ask_date { get; set; }

}

And I get *"Store update, insert, or delete statement affected an unexpected number of rows (0). * every time

What am I doing wrong?

MoShe
  • 6,197
  • 17
  • 51
  • 77
  • Nothing obviously wrong here. Is this the exact code that causes the exception? – Gert Arnold Feb 05 '14 at 23:03
  • Exact code! I'm using azure SQL – MoShe Feb 05 '14 at 23:11
  • Where is _ctx defined? – Scottie Feb 05 '14 at 23:14
  • AnyContext type that inherent from dbContext – MoShe Feb 05 '14 at 23:32
  • Yes, but it looks like AnyContact has 2 constructors. One that is empty and one that takes a string. The empty constructor does not call base DBContext, while the string constructor does. My guess is this is your problem. Can you modify AnyContext() to AnyContext() : base()? Edited: Come to think of it, AnyContext() is missing a lot of stuff that AnyContext(string connectionString) has. – Scottie Feb 05 '14 at 23:42
  • See my edited DbContext it as you suggest but still the same issue – MoShe Feb 06 '14 at 18:01
  • Have you seen this question? http://stackoverflow.com/questions/1836173/ – Aaron Palmer Feb 06 '14 at 18:12
  • @AaronPalmer - Yes I read it but it is not seems to be relevant to my case . I send one request through REST – MoShe Feb 06 '14 at 18:27

0 Answers0