5

I have a class like this:

[Table("member_activation")]
public partial class MemberActivation
{
    [Key]
    public Int64 member_id { get; set; }
    public String token { get; set; }
}

My db:

public class SMADbContext : DbContext
{
    public SMADbContext() : base("SMADB")
    {
        Database.SetInitializer<SMADbContext>(new NullDatabaseInitializer<SMADbContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Member> Members { get; set; }
    public DbSet<MemberActivation> MemberActivations { get; set; }
    public DbSet<ApiAccount> ApiAccounts { get; set; }
    public DbSet<ApiHardware> ApiHardwares { get; set; }
    public DbSet<MemberRelation> MemberRelations { get; set; }
}

In my controller:

    [Route("tester")]
    [AllowAnonymous]
    public IHttpActionResult tester()
    {
        using (var db = new SMADbContext())
        {
            var memberActivation = new MemberActivation();
            memberActivation.member_id = 10155;
            memberActivation.token = "hello";

            db.MemberActivations.Add(memberActivation);
            return Json(new { dbset = db.MemberActivations.ToList(), memberAct = memberActivation });

        }
    }

db.MemberActivations.Add(memberActivation); does not work. When I return the json, the dbset does not include the newly created memberActivation. I do not have db.SaveChanges() because it will not save until the memberActivation is pushed to the dbset

user3861672
  • 55
  • 1
  • 1
  • 5
  • 9
    Don't forget to call `db.SaveChanges();` :) – Anthony Chu Jul 21 '14 at 19:12
  • When I include db.SaveChanges() directly after db.MemberActivations.Add(memberActivation), it does not work and says I cannot input Null values inside member_id. @Anthony Chu – user3861672 Jul 21 '14 at 19:15
  • If you don't really want to save to the database, perhaps db.AcceptChanges() is what you need. – Moby Disk Jul 21 '14 at 19:27
  • My dbset never adds the entity... And db.AcceptChanges() doesn't exist. How is it that my entity is not added to the dbset?! @Moby Disk – user3861672 Jul 21 '14 at 19:39

1 Answers1

4

You cant set member_id, it is the key and ef uses it as identity. It will be ignored. You can configure ef so that member_id is not identity but that's another topic.

 db.MembershipActivations.Add( new MemberActivation { token = "hello"}):
 db.SaveChanges();

should work fine.

if however , as it would appear , you have an existing member and you are trying to set a relationship with that entity via a join table. Then you should retrieve that entity and set the memberactivation. Ef will sort the rest out for you. Bit of guessing here as i would need to see the involved entities.

  • I'm starting to understand the conflict. But, like you had mentioned, the member id is generated in another table(Members). member_id in the MemberActivation table is a primary key and a foreign key from the Member table(id). I did not configure it this way, I just have to work with it. How can I save it knowing that it is not an identity column? @RichardFrance – user3861672 Jul 22 '14 at 13:24
  • if you could add the other classes to your question, that would help greatly. – Richard France Jul 22 '14 at 14:11
  • 3
    I figured it out. Must add [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)] data annotation to member_id property @RichardFrance – user3861672 Jul 22 '14 at 14:49
  • in EF Core the same issue occurs, you would need to use ValueGeneratedNever() on the property to get it work. – Stefan de Kok Feb 10 '19 at 23:24