Я пытаюсь создать свой первый проект с MVC Asp.net.Внутри моего контроллера у меня есть ActionResult со строковым параметром «Имя пользователя», который восстанавливает все записи и пытается связать его с моделью и впоследствии с представлением, но эта строка выдает мне ошибку:
model.Utenti = myRecord;
Здесьall ActionResult:
public ActionResult Test(string Username)
{
using (DatabaseContext val = new DatabaseContext())
{
var model = new UtenteUpdModel();
var myRecord = (from c in val.Utenti where c.Username == Username select c).FirstOrDefault();
model.Utenti = myRecord; <<<<<<<<<<<<<<<<<< PROBLEM
return View(model);
}
}
Ошибка говорит: Невозможно неявно преобразовать тип 'WebApplication.Models.Utente' в 'System.Data.Entity.DbSet (WebApplication.Models.Utente>'
Visual studio предлагает изменить автоматически сгенерированный код (Utente.cs) с помощью еще 2-х иструкций:
using System.Data.Entity;
и этого:
public static implicit operator DbSet<object>(Utente v)
{
throw new NotImplementedException();
}
Вот полный контентфайла (Utente.cs) обновлено:
//------------------------------------------------------------------------------
// <auto-generated>
// Codice generato da un modello.
//
// Le modifiche manuali a questo file potrebbero causare un comportamento imprevisto dell'applicazione.
// Se il codice viene rigenerato, le modifiche manuali al file verranno sovrascritte.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WebApplication.Models
{
using System;
using System.Collections.Generic;
using System.Data.Entity;
public partial class Utente
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Utente()
{
this.Link_Utenti_x_Servizi = new HashSet<Link_Utenti_x_Servizi>();
this.LogAccessi = new HashSet<LogAccessi>();
}
public int ID { get; set; }
public System.Guid UserID { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string PasswordSalt { get; set; }
public bool isAbilitato { get; set; }
public string Cell { get; set; }
public Nullable<int> lingua { get; set; }
public Nullable<int> IDclasse { get; set; }
public bool isAdministrator { get; set; }
public Nullable<System.DateTime> ExpireDate { get; set; }
public System.DateTime insertTime { get; set; }
public virtual ClassiUtenti ClassiUtenti { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Link_Utenti_x_Servizi> Link_Utenti_x_Servizi { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LogAccessi> LogAccessi { get; set; }
public static implicit operator DbSet<object>(Utente v)
{
throw new NotImplementedException();
}
}
}
Я последовал предложению, но проблема сохраняется.
Здесь также полная модель:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication.Models
{
public class UtenteUpdModel : DatabaseContext
{
[Key]
public int Id { get; set; }
public string UserID { get; set; }
[Required]
[StringLength(50, ErrorMessage = "Username troppo lungo (max 50 caratteri)")]
public string Username { get; set; }
[Required]
[EmailAddress]
[StringLength(150, ErrorMessage = "Email troppo lunga (max 150 caratteri)")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
public string Password { get; set; }
public bool IsAbilitato { get; set; }
public string Cell { get; set; }
public int Lingua { get; set; }
public int IDClasse { get; set; }
public bool IsAdministrator { get; set; }
public System.DateTime ExpireDate { get; set; }
public System.DateTime InsertTime { get; set; }
public IEnumerable<ClassiUtenti> Classi { get; set; }
public IEnumerable<Servizi> ElencoServizi { get; set; }
public int ID_utente { get; set; }
public System.DateTime InsertDate { get; set; }
}
}
кто-тоесть идеи как решить? Пожалуйста.