Add an entry on a virtual property - asp.net-mvc

I'm new to asp.net & mvc entity framework.
I'm doing a backoffice for managing applications
Here is my App class:
public class AsyApp
{
[Key]
public int AsyAppId { get; set; }
[StringLength(255)]
[Required]
[DisplayName("Nom")]
public string Name { get; set; }
[StringLength(255)]
[Required]
[RegularExpression(#"com\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+", ErrorMessage = "Le Bundle n'est pas de la bonne forme (ex: com.Company.AppName)")]
public string Bundle { get; set; }
public virtual Theme Theme { get; set; }
public virtual AppIdentity AppIdentity { get; set; }
public virtual ICollection<Scene> Scenes { get; set; }
public virtual ICollection<FilePath> FilePaths { get; set; }
}
The app has an AppIdentity attached to it:
public class AppIdentity
{
[DisplayName("Ecran d'accueil")]
public virtual FilePath Splashscreen { get; set; }
[DisplayName("Icon")]
public virtual FilePath Icon { get; set; }
[DisplayName("Logo Application")]
public virtual FilePath LogoApp { get; set; }
[DisplayName("Logo Client")]
public virtual FilePath LogoClient { get; set; }
[Key, ForeignKey("AsyApp")]
public int AsyAppId { get; set; }
public virtual AsyApp AsyApp { get; set; }
}
And here is my dbcontext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<AsyApp> AsyApps { get; set; }
public DbSet<Theme> Themes { get; set; }
public DbSet<Scene> Scenes { get; set; }
public DbSet<AppIdentity> AppIdentities { get; set; }
public DbSet<FilePath> FilePaths { get; set; }
}
I have a view where I edit the AppIdentity of the application.
I would like to update for instance the Splashscreen property.
The behavior that I expect is that when I do
appIdentity.Splashscreen = new FilePath();
It add an entry on the FilePaths dbset;
Set the AsyAppId of the new filepath entry to the current AsyApp.
But It doesn't work...
Here is my controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(AppIdentity appIdentity)
{
var f = new FilePath();
f.AsyAppId = appIdentity.AsyAppId;
db.FilePaths.Add(f);
db.SaveChanges();
appIdentity.Splashscreen = f;
db.SaveChanges();
return RedirectToAction("Edit", "AppIdentity", new { asyAppId = appIdentity.AsyAppId });}}
Any help ?
Thank you in advance, this will help me a lot.
I just don't fully understand how virtual properties works right know

Try putting this code after appIdentity.Splashscreen = f; :
db.AppIdentities.Attach(appIdentity);
context.Entry(appIdentity).State = EntityState.Modified;
db.SaveChanges();

Related

Missing type map configuration or unsupported mapping, one to one relationship

I have relationship one to one
public class Book
{
public int BookId { get; set; }
public string Name { get; set; }
public string Annotation { get; set; }
public virtual File File { get; set; }
public int? SeriesId { get; set; }
public DateTime UploadDate { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual ICollection<Mark> Marks { get; set; }
public Book()
{
Comments = new List<Comment>();
Authors = new List<Author>();
Genres = new List<Genre>();
}
}
public class File
{
[Key,ForeignKey("Book")]
public int BookId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual Book Book { get; set; }
}
And I want to transfer data to classes:
public class BookDO
{
public int BookId { get; set; }
public string Name { get; set; }
public string Annotation { get; set; }
public virtual FileDO File { get; set; }
}
public class FileDO
{
public int BookId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual BookDO Book { get; set; }
}
in such way:
var books = Database.Books.GetAll().ToList();
Mapper.Initialize(cf => cf.CreateMap<Book, BookDO>());
return Mapper.Map<List<Book>, List<BookDO>>(books);
but i'm getting Missing type map configuration or unsupported mapping.
Mapping types:
File -> FileDO
Domain.File -> BusinessLogic.Data_Objects.FileDO
Maybe i need to initialize one more mapper to map File to FileDO or modify existing mapper configuration? help me please.
Yes, you also need to create a map for File -> FileDo. This map must be configured for the same mapper as used for Book -> BookDo.
It is good practice to wrap your mapping configuration into an AutoMapper.Profile:
using AutoMapper;
public class BookMappingProfile: Profile {
public BookMappingProfile() {
CreateMap<Book, BookDo>();
CreateMap<File, FileDo>();
}
}
And then initialize the mapper with these profiles:
Mapper.Initialize(cfg => {
cfg.AddProfile<BookMappingProfile>();
cfg.AddProfile<MyOtherProfile>();
});

How to set the one-to-one relationship with fluent api in this case? (EF6)

I have this two entities:
public partial class Ficheros
{
public Guid Idfichero { get; set; }
public long Iddocumento { get; set; }
public byte[] Fichero { get; set; }
public virtual Documentos IddocumentoNavigation { get; set; }
}
public partial class Documentos
{
public Documentos()
{
ElementosDocumentos = new HashSet<ElementosDocumentos>();
}
public long Iddocumento { get; set; }
public string Nombre { get; set; }
public long? IdtipoDocumento { get; set; }
public string Codigo { get; set; }
public decimal? Espacio { get; set; }
public string Unidades { get; set; }
public long? Bytes { get; set; }
public virtual ICollection<ElementosDocumentos> ElementosDocumentos { get; set; }
public virtual Ficheros Ficheros { get; set; }
public virtual DocumentosTipos IdtipoDocumentoNavigation { get; set; }
}
In the database, IDFichero is an uniqueidentifier and in Documentos the IDDocumento is a big int autoincrement. The main table is Documentos, that has one and only one fichero, and it is requiered.
The examples that I have seen, it would make me that IDFichero was IDDocumento, but to store a file in the database I need that the ID is a uniqueidentifier.
Thanks.
The relationship you are describing in EF terms is one-to-one FK association with both ends required, Documentos being the principal and Ficheros the dependent.
EF does not support explicit FK for this type of association, so start by removing the Ficheros.Iddocumento property:
public partial class Ficheros
{
public Guid Idfichero { get; set; }
public byte[] Fichero { get; set; }
public virtual Documentos IddocumentoNavigation { get; set; }
}
then use the following fluent configuration:
modelBuilder.Entity<Documentos>()
.HasRequired(e => e.Ficheros)
.WithRequiredPrincipal(e => e.IddocumentoNavigation)
.Map(m => m.MapKey("Iddocumento"));

Many to Many Relationship in MVC 5 EF6

Im using MVC 5 and EF 6.
There are 3 tables in my code: "Leads", "Phones", "LeadStatus" and a junction table "LeadPhones".
I want to show following properties in Index View:
Leads.FullName
LeadStatus.Status
Phones.PhoneID
How should I configure the LeadController?
public partial class Leads
{
public Leads()
{
this.LeadPhones = new HashSet<LeadPhones>();
}
[Key]
public int LeadID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
public int LeadStatusID { get; set; }
public virtual LeadStatus LeadStatus { get; set; }
public virtual ICollection<LeadPhones> LeadPhones { get; set; }
}
and
public partial class Phones
{
public Phones()
{
this.AccountPhones = new HashSet<AccountPhones>();
this.ContactPhones = new HashSet<ContactPhones>();
this.EmployeePhones = new HashSet<EmployeePhones>();
this.LeadPhones = new HashSet<LeadPhones>();
}
[Key]
public int PhoneID { get; set; }
public string PhoneNo { get; set; }
public virtual ICollection<LeadPhones> LeadPhones { get; set; }
}
and
public partial class LeadPhones
{
[Key]
public int LeadPhoneID { get; set; }
public int LeadID { get; set; }
public int PhoneID { get; set; }
public virtual Leads Leads { get; set; }
public virtual Phones Phones { get; set; }
}
and
public partial class LeadStatus
{
public LeadStatus()
{
this.Leads = new HashSet<Leads>();
}
[Key]
public int LeadStatusID { get; set; }
public string Status { get; set; }
public virtual ICollection<Leads> Leads { get; set; }
}
the following does not work:
public ActionResult Index()
{
var leads = db.Leads.Include(l => l.LeadStatus).Include(l => l.LeadPhones);
return View(leads);
}
The main problem is the "PhoneNo". How could I get that from a junction table?
Remove the LeadPhoneID column from the LeadPhones table -- It isn't necessary and will only cause problems for you. Remove all references to LeadPhones in your classes. Leads a virtual property to Phones (not LeadPhones). LeadPhones has a virtual property to Leads (not LeadPhones).
After you do that, then you should be able to:
var leads = db.Leads.Include(l => l.LeadStatus).Include(l => l.Phones);
public partial class Lead
{
public Lead()
{
this.Phones = new HashSet<Phone>();
}
[Key]
public int LeadID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
public int LeadStatusID { get; set; }
public virtual LeadStatus LeadStatus { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
public partial class Phone
{
public Phone()
{
this.AccountPhones = new HashSet<AccountPhones>();
this.ContactPhones = new HashSet<ContactPhones>();
this.EmployeePhones = new HashSet<EmployeePhones>();
this.Leads = new HashSet<Lead>();
}
[Key]
public int PhoneID { get; set; }
public string PhoneNo { get; set; }
public virtual ICollection<Lead> Leads { get; set; }
}
public partial class LeadStatus
{
public LeadStatus()
{
this.Leads = new HashSet<Lead>();
}
[Key]
public int LeadStatusID { get; set; }
public string Status { get; set; }
public virtual ICollection<Lead> Leads { get; set; }
}
I've also corrected the plurality of your classes. Lead vs Leads. The class describes a single lead, so it should be called Lead not Leads. Phone vs Phones.

Model or ViewModel in a ListViewModel

Should T be a for example Customer or CustomerViewModel ?
The annotations bound to Mvc namespace are on the ListViewModel so actually I could pass the Customer object. What do you think?
public class ListViewModel<T>
{
[Required(ErrorMessage="No item selected.")]
public int[] SelectedIds { get; set; }
public IEnumerable<T> DisplayList { get; set; }
}
UPDATE
[HttpGet]
public ActionResult Open()
{
IEnumerable<Testplan> testplans = _testplanDataProvider.GetTestplans();
OpenTestplanListViewModel viewModel = new OpenTestplanListViewModel(testplans);
return PartialView(viewModel);
}
public class OpenTestplanListViewModel
{
public OpenTestplanListViewModel(IEnumerable<Testplan> testplans)
{
var testplanViewModels = testplans.Select(t => new TestplanViewModel
{
Name = string.Format("{0}-{1}-{2}-{3}", t.Release.Name, t.Template.Name, t.CreatedAt, t.CreatedBy),
TestplanId = t.TestplanId,
});
DisplayList = testplanViewModels;
}
[Required(ErrorMessage = "No item selected.")]
public int[] SelectedIds { get; set; }
public string Name { get; set; }
public IEnumerable<TestplanViewModel> DisplayList { get; private set; }
}
public class TestplanViewModel
{
public int TestplanId { get; set; }
public string Name { get; set; }
}
public class Testplan
{
public int TestplanId { get; set; }
public int TemplateId { get; set; }
public int ReleaseId { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
public Template Template { get; set; }
public Release Release { get; set; }
}
T should ideally be a view model. Having a view model referencing domain models is some kind of a hybrid view model, not a real one. But if you think that in this specific case the domain model will be exactly the same as the view model then you could keep it as well.

multiple "1 to 0..1" relationship models

I am using this tutorial from microsoft to create a one-zero-to-one relationship with EF4.1 Between an Instructor and OfficeAssignment. This is working like a charm.
But now I want to add a Home for each Instructor (1 to zero-or-1) like in this:
I added the Home model exactly the same way as the OfficeAssignment (like in the tutorial above), but when I try to add controllers for these model, I get the error "An item with the same name has already been added".
So my model is set up incorrectly.
What is wrong with the below?
How do I create multiple one-to-zero-to-one relationships in EF4.1?
public class Instructor
{
public Int32 InstructorID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual OfficeAssignment OfficeAssignment { get; set; }
public virtual Home Home { get; set; }
}
public class OfficeAssignment
{
[Key]
public int InstructorID { get; set; }
public string Location { get; set; }
public virtual Instructor Instructor { get; set; }
}
public class Home
{
[Key]
public int InstructorID { get; set; }
public string Location { get; set; }
public virtual Instructor Instructor { get; set; }
}
public class Context : DbContext
{
public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
public DbSet<Instructor> Instructors { get; set; }
public DbSet<Home> Homes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Instructor>()
.HasOptional(p => p.OfficeAssignment)
.WithRequired(p => p.Instructor);
modelBuilder.Entity<Instructor>()
.HasOptional(p => p.Home).WithRequired(p => p.Instructor);
}
Doesn't look like EF supports real 1 to 0..1 relationship. You need a foreign key. And add the optional (int?) into the main model.
So I did this as follow, and it works like a charm.
public class Instructor
{
public Int InstructorID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public int? OfficeAssignmentID { get; set; }
public virtual OfficeAssignment OfficeAssignment { get; set; }
public int? HomeID { get; set; }
public virtual Home Home { get; set; }
}
public class OfficeAssignment
{
public int OfficeAssignmentID { get; set; }
public string Location { get; set; }
}
public class Home
{
public int HomeID { get; set; }
public string Location { get; set; }
}

Resources