0

There is automatically created database for authorize users. I update my database my local tables. There are some tables like Klient, Ulica, Address. Models who describe this tables:

public partial class Klient
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Klient()
    {
        this.Abonements = new HashSet<Abonement>();
    }

    public int ID_klient { get; set; }
    public string FIO_klient { get; set; }
    public string Telephon { get; set; }
    public Nullable<int> ID_adress { get; set; }
    public string ID_Login { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Abonement> Abonements { get; set; }
    public virtual Adress Adress { get; set; }
}
public partial class Ulica
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Ulica()
    {
        this.Adresses = new HashSet<Adress>();
    }

    public int ID_ulica { get; set; }
    public string Nazvanie_ulicu { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Adress> Adresses { get; set; }
}
public partial class Adress
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Adress()
    {
        this.Klients = new HashSet<Klient>();
        this.Sotrudniks = new HashSet<Sotrudnik>();
    }

    public int ID_adress { get; set; }
    public Nullable<int> Nomer_doma { get; set; }
    public Nullable<int> Nomer_kvartiru { get; set; }
    public Nullable<int> ID_ulica { get; set; }

    public virtual Ulica Ulica { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Klient> Klients { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Sotrudnik> Sotrudniks { get; set; }
}

The DbContext of this model is :

public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Adress> Adresses { get; set; }
    public virtual DbSet<Klient> Klients { get; set; }
    public virtual DbSet<Ulica> Ulicas { get; set; }
    // and others models of my database
}

When I try change standart AccountController and add some data to model Klient on Register method, I add this code to Register method:

public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
              // some code
              var ulica = new Ulica() { Nazvanie_ulicu = model.Ulica };
                FitnesManager.Ulicas.Add(ulica);
                var adress = new Adress { Ulica = ulica, ID_ulica = ulica.ID_ulica, Nomer_doma = model.Nomer_doma, Nomer_kvartiru = model.Nomer_kvartiru};
                FitnesManager.Adresses.Add(adress);
                var klient = new Klient { FIO_klient = model.FIO, Telephon = model.Telephon, Adress = adress, ID_adress = adress.ID_adress, ID_Login = user.Id };
                var res = FitnesManager.Klients.Add(klient);
                await FitnesManager.SaveChangesAsync();
        }

        // Появление этого сообщения означает наличие ошибки; повторное отображение формы
        return View(model);
    }

FitnesManager is DbContext value of AccountController

public Entities FitnesManager
    {
        get
        {
            return _fitnesManager ?? new Entities();
        }
        private set
        {
            _fitnesManager = value;
        }
    }

But when I call Register method any data do not add to database. How do I fix it?

1 Answer 1

1

This line is wrong:

return _fitnesManager ?? new Entities();

You are returning a new instance every time. I think you were going for this style:

return _fitnesManager ?? (_fitnesManager = new Entities());

Either that, or just do this:

if (_fitnesManager == null)
   _fitnesManager = new Entities();
return _fitnesManager;
Sign up to request clarification or add additional context in comments.

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.