Buona sera Moreno,
dovrei approfittare ancora una volta della tua conoscenza....
con gli aiuti che mi hai dato tutto ok e ti ringrazio, adesso oltre alla tabella appuntamenti che funziona senza problemi sto inserendo le note quindi la possibilita di assegnare più note ad ogni utente, quando però creo il controller note (che ho impostato esattamente come appuntamenti) e creo un nuova nota al momento della creazione mi da un errore:
Riferimento a un oggetto non impostato su un'istanza di oggetto. L'errore lo segnala nel controller nota in questo punto...
Riga 58: }
Riga 59:
Riga 60: ViewBag.UserId = new SelectList(db.Users, "Id", "Nome", nota.UserId);
Riga 61: return View(nota);
Riga 62: }
Ma il controller appuntamenti e le caratteristiche nell'identity sono le stesse identiche ma questo non funziona non sono perchè... ti allego il codice dell'identity di entrambi:
[Table("Appuntamenti")]
public class Appuntamento
{
[Key]
public int idAppuntamento { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public string Descrizione { get; set; }
public int idServizio { get; set; }
[ForeignKey("idServizio")]
public Servizio servizi { get; set; }
public DateTime Start_Data { get; set; }
public DateTime End_Data { get; set; }
public string ThemeColor { get; set; }
public bool IsFullDay { get; set; }
}
[Table("Note")]
public class Nota
{
[Key]
public int idNota { get; set; }
public string nota { get; set; }
public DateTime dataIns { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
public string Nome { get; set; }
public string Cognome { get; set; }
public string Telefono { get; set; }
public virtual Appuntamento Appuntamenti { get; set; }
public virtual Nota Note { get; set; }
public virtual ICollection<Card> Cards { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Tenere presente che il valore di authenticationType deve corrispondere a quello definito in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Aggiungere qui i reclami utente personalizzati
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Nota> Note { get; set; }
public DbSet<Animale> Animali { get; set; }
public DbSet<Promozione> Promozioni { get; set; }
public DbSet<Spesa> Spese { get; set; }
public DbSet<Prodotto> Prodotti { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<Appuntamento> Appuntamenti { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// one-to-zero or one relationship between ApplicationUser and Customer
// UserId column in Customers table will be foreign key
modelBuilder
.Entity<Appuntamento>()
.HasRequired(m => m.ApplicationUser)
.WithMany()
.HasForeignKey(a => a.UserId);
modelBuilder
.Entity<Nota>()
.HasRequired(m => m.ApplicationUser)
.WithMany()
.HasForeignKey(a => a.UserId);
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public System.Data.Entity.DbSet<Toeletta10.Models.Servizio> Servizi { get; set; }
}
}
il controller appuntamenti che
funziona :
// GET: Appuntamenti/Create
public ActionResult Create()
{
ViewBag.UserId = new SelectList(db.Users, "Id", "Nome");
ViewBag.idServizio = new SelectList(db.Servizi, "idServizio", "nomeServizio");
return View();
}
// POST: Appuntamenti/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "idAppuntamento,UserId,Descrizione,idServizio,Start_Data,End_Data,ThemeColor,IsFullDay")] Appuntamento appuntamento)
{
if (ModelState.IsValid)
{
db.Appuntamenti.Add(appuntamento);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.Users, "Id", "Nome", appuntamento.UserId);
ViewBag.idServizio = new SelectList(db.Servizi, "idServizio", "nomeServizio", appuntamento.idServizio);
return View(appuntamento);
}
e il controller Note che
NON FUNZIONA :
// GET: Note/Create
public ActionResult Create()
{
ViewBag.UserId = new SelectList(db.Users, "Id", "Nome");
return View();
}
// POST: Note/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "idNota,nota,dataIns,UserId")] Nota nota)
{
if (ModelState.IsValid)
{
db.Note.Add(nota);
db.SaveChanges();
return RedirectToAction("Index");
}
// Qui segnala l'errore
ViewBag.UserId = new SelectList(db.Users, "Id", "Nome", nota.UserId);
return View(nota);
}
so del consiglio che mi hai dato di usare solo le fluent api ma le sto studiando poi le sostituirò tutte.
grazie mille del tuo tempo
Mirko