-1

I am going to get mad. I have a so simple code but it doesn't work.

I have tested every thing and I can't understand the reason.

The code is like this:

FataDBEntities model = new FataDBEntities();

UserAccount nua = new UserAccount();

nua.username = username;
nua.pass = password;
nua.email = mailaddress;
nua.activated = false;

model.UserAccounts.Add(nua);

try
{
    model.SaveChanges();
}
catch (Exception)
{
    throw;
}

string activationCode;

try
{
    activationCode = genActivationCodeAndMail(mailaddress, username);
}
catch (Exception)
{
    throw;
}

ActivationCode ac = new ActivationCode();

ac.code = activationCode;
ac.expiration_date = DateTime.Now.AddDays(1);
nua = model.UserAccounts.Single(p => p.username == username);
ac.wichUser = nua.ID;

model.ActivationCodes.Add(ac);

try
{
    model.SaveChanges();
}
catch (Exception)
{
    throw;
}

and the model is like this :

enter image description here

The error is with second insertion ... the first one works fine.

Additional information: Unable to update the EntitySet 'ActivationCode' because it has a DefiningQuery and no element exists in the element to support the current operation.

Please help...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ConductedClever
  • 4,175
  • 2
  • 35
  • 69

1 Answers1

3

According to this SO article, this error can occur if the entity set is mapped from the database view, custom database query or if database table does not have primary key.

This has happened to me before, and I used that same article to fix the issue :)

Community
  • 1
  • 1
jwatts1980
  • 7,254
  • 2
  • 28
  • 44